I would like to get my data out from the sqlite db.each function using the promise object and the async/await I tried to do it but I don't really understand how to do it and I would like some explanations
my code below :
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./data.sqlite', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database');
});
function request (req) {
new Promise(function (resolve, reject) {
db.each(req, function (err, row) {
if (err)
reject(err);
else
data.push(row);
}, function (err, n) {
if (err) {
reject(err);
}
else
resolve(data);
});
});
}
data = await request("select * from user "); //I'm getting errors here
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection');
});
await can only be used inside an async function. Create an async function, inside this function, await the promise returned by request function
async function makeRequest() {
data = await request("select * from user ");
}
and you need to return the promise from request function so that it can be awaited
function request (req) {
return new Promise(function (resolve, reject) {
// code
});
}
Related
I need to create a render method to build html blocks into a string.
It seems to work but I used the notorious "new Promise" and I wanted to know if what I've done is either correct or not:
async render(req, res) {
const locals = await this.execute(req); // DB operation, retrieve context
return new Promise((resolve, reject) => {
try {
return res.render('my-view', { ...locals, layout: null }, (err, html) => {
if (err) {
return reject(err);
}
return resolve(html);
});
} catch (err) {
return reject(err);
}
});
}
Thank you!
The new Promise constructor implicitly catches (synchronous) exceptions from the executor callback, so that try/catch is not needed. Also, the return value is ignored. You'd write just
async render(req, res) {
const locals = await this.execute(req); // DB operation, retrieve context
return new Promise((resolve, reject) => {
res.render('my-view', { ...locals, layout: null }, (err, html) => {
if (err) reject(err);
else resolve(html);
});
});
}
When i call rest API and return response it show undefined but i console.log this response it return
var request = require("request");
function initialize() {
// Setting URL and headers for request
var options = {
url: 'http://postalpincode.in/api/pincode/400605',
json: true
};
// Return new promise
return new Promise(function (resolve, reject) {
// Do async job
request.get(options, function (err, resp, body) {
if (err) {
reject(err);
} else {
resolve(JSON.stringify(body));
}
})
})
}
function main() {
var initializePromise = initialize();
initializePromise.then(function (result) {
return result;
})
}
console.log('', main())
But when i console log this response it show output correct
var request = require("request");
function initialize() {
// Setting URL and headers for request
var options = {
url: 'http://postalpincode.in/api/pincode/400605',
json: true
};
// Return new promise
return new Promise(function (resolve, reject) {
// Do async job
request.get(options, function (err, resp, body) {
if (err) {
reject(err);
} else {
resolve(JSON.stringify(body));
}
})
})
}
function main() {
var initializePromise = initialize();
initializePromise.then(function (result) {
console.log('', result)
})
}
console.log('', main())
I want When i call rest API and return response it show correct output
The return inside the thenscope is not returning for the function main, but only for the thenscope of the promise. You need to return the promise like so:
function main() {
var initializePromise = initialize();
return initializePromise.then(function (result) {
return result;
})
}
main().then((result) => console.log('',result));
you can't make a sync function call an async method and expect to have get its result.
use async/await
async function main() {
var initializePromise = await initialize();
console.log(initializePromise)
}
My question is, why are you wrapping in a new Promise something that's already from a return type of Promise?
You could just do:
request.get(endpoint, options).then((response) => console.log(response)).catch((error) => console.log(error));
Let me know what's the output in that case.
The then resolution of initializePromise method resolves at a later stage when a response is fetched from REST HTTP call i.e. it does not get returned when you call main() method due to the fact it is async. To handle such code, you should either use a callback
function main(completionHandler) {
var initializePromise = initialize();
initializePromise.then(function (result) {
completionHandler(result);
})
}
main((result) => { console.log(result)})
or a promise
function main() {
// Return new promise
return new Promise(resolve => {
var initializePromise = initialize();
initializePromise.then(function (result) {
resolve(result);
})
}
}
main().then(result => console.log(result));
return new Promise(function (resolve, reject) {
// Do async job
request.get(options, function (err, resp, body) {
if (err) {
reject(err);
} else {
try {
resolve(JSON.stringify(body));
} catch(e) {
reject(e);
}
}
})
})
in main function:
function main() {
initialize().then((result) => {
console.log(result);
return result;
}).catch((err) => {
console.log(err);
return err;
})
}
I would like to make an Ajax request to my MongoDB server and use the data along with other async tasks using a standalone function so that I can modularize my code as much as possible. I am not very experienced with async programming so I might be doing some basic mistake. In my code, I used another function (doubleAfter2Seconds) returning a promise, which works fine. The result variable from await getMongoData("mydb", url) is outputted as undefined instead of the actual data.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://{MyServer}:27017/";
function getMongoData(dboArg, urlArg) {
var myPromise = () => {
return new Promise(resolve => {
MongoClient.connect(urlArg, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db(dboArg);
dbo.collection(myCollection).find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
resolve(result[0]);
});
})
})
}
}
function doubleAfter2Seconds(x) {
return new Promise(resolve => {
console.log("v");
setTimeout(() => {
resolve(x * 2);
}, 1000);
});
}
async function addAsync(x) {
// This works
const a = await doubleAfter2Seconds(10);
console.log(a);
// This doesn't work
result = await getMongoData("mydb", url);
console.log(result);
return x;
}
addAsync(10).then((sum) => {
console.log(sum);
});
Here is the corrected getMongoData function based on the comments
function getMongoData(dboArg, urlArg) {
return new Promise(resolve => {
MongoClient.connect(urlArg, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db(dboArg);
dbo.collection(myCollection).find({}).toArray(function(err, data) {
err
? reject(err)
: resolve(data[0]);
});
})
})
}
I am new in node js and i need result of my first query as a parameter in another function.
Here my model:
return new Promise((resolve, reject) => {
var sql = "SELECT * FROM table";
db.query(sql,
(err, result) => {
if (err) {
return reject(err)
}
return resolve(result)
});
});
I need something like this
return new Promise((resolve, reject) => {
var sql = "SELECT * FROM table";
db.query(sql)
.then(rows.map(row =>{
return arr[row.id] = this.getProduct(row.id);
}))
})
Use Async/Await.
You can make a generic queryDb function which will call db and fetch result for any query.
mainFunction will wait for await to finish and fetch result which you can use in line below it.
Code also looks clean and easy to understand.
Something like this,
async function mainFunction(){
try{
const query = `INSERT INTO aaa xxxxxx`;
const result = await queryDb(query);
//do whatever with the result;
this.getProduct(result);
}
catch(e){
//catch error
}
}
function queryDb(sql){
return new Promise( ( resolve, reject ) => {
db.query(sql,
(err, result) => {
if (err) {
return reject(err)
}
return resolve(result)
});
})
}
I want to execute four blocks of code sequentially and conduct test during this sequential execution. The challenge is that the blocks contain calls of asynchronous functions.
I cannot seem to wrap my head around promises to use them properly and I just seem stuck after spending several hours on the block of code.
// Store current log of drone visits for DJI Phantom 4
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
console.log(err);
}
else {
current_drone_visits = rows[0]['COUNT(drone_id)'];
}
});
it("should return status 200 (OK) when requesting route for this function", function(done) {
request.get("/product/DJI/Phantom_4").query({brand: dji_brand, model: dji_model}).end(function(err, res) {
assert.equal(res.status, 200);
done();
});
});
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
console.log(err);
}
else {
updated_drone_visits = rows[0]['COUNT(drone_id)'];
}
});
it("should increment the value of drones visited in the database", function(done) {
console.log("A - " + current_drone_visits);
console.log("B - " + updated_drone_visits);
assert.equal(current_drone_visits + 1, updated_drone_visits);
done();
});
What should I do here if I want to chain my callbacks such that they execute only after the previous function has finished?
Use async.js if you are comfortable with callbacks, or use Promises, you can chain them in order to synchronise you functions.
just wrap your callback in a function that return a promise like this
function P(){
return new Promise((resolve, reject)=>{
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) reject(err);
else {
resolve(rows[0]['COUNT(drone_id)'])
}
});
})
}
function T1(data){
//data in this case will be the result of the P() function
return new Promise((resolve, reject)=>{
request.get("/product/DJI/Phantom_4").query({brand: dji_brand, model: dji_model}).end(function(err, res) {
if(err || res.status==200) reject(err);
resolve();
});
})
}
P()
.then(T1)
.then(...)
.catch((err)=>{
//handle error here
})
sry for the bad formatting
What should I do here if I want to chain my callbacks such that they
execute only after the previous function has finished.
This code does it for you:
describe ('test', function () {
this.timeout(6000);
it ('test', function(done) {
var query1 = new Promise (function (resolve, reject) {
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
reject(new Error('rejected query1'));
} else {
var current_drone_visits = rows[0]['COUNT(drone_id)'];
resolve(current_drone_visits);
}
});
})
.then(function(current_drone_visits){
var request1 = new Promise (function (resolve, reject) {
request.get("/product/DJI/Phantom_4").query({brand: dji_brand, model: dji_model}).end(function(err, res) {
if (err) {
reject(new Error('rejected request'));
} else {
resolve(res);
}
})
})
.then(function (res) {
try {
assert.equal(res.status, 200);
return current_drone_visits;
}
catch (err) {
done (err);
}
})
.catch(function(err) {
return Promise.reject(err);
})
return request;
})
.then(function(current_drone_visits) {
var query2 = new Promise (function (resolve, reject) {
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
reject(new Error('rejected query2'))
} else {
resolve();
}
})
})
.then(function () {
var updated_drone_visits = rows[0]['COUNT(drone_id)'];
var bundled = [current_drone_visits, updated_drone_visits];
return bundled;
})
.catch(function(err) {
return Promise.reject(err);
})
return query2;
})
.then(function(bundled) {
var current_drone_visits = bundled[0];
var updated_drone_visits = bundled[1];
console.log('A - ' + current_drone_visits);
console.log('B - ' + updated_drone_visits);
assert.equal(current_drone_visits + 1, updated_drone_visits);
done()
})
.catch (function (err) {
done(err);
})
})
})
You can use the this.timeout(6000) to determine how long Mocha will wait for your asynchronous code to complete. The default value is 2000 ms which may or may not be sufficient in your case.