not able to get items. it return [] . but it show correct on console.log(item). i think before my map() runs complete. it print all data. how to solve this issue. i am new in node.
function getBlockUsers() {
return new Promise(function (resolve, reject) {
BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,result) {
if(err){
reject({"msg":"failed to getting block user."})
}else{
var results = [];
result.map(function(item){
Vehicle.findOne({userId:item.blockedId}).lean().exec(function(err,vehicle){
if(vehicle){
item.vehicleId = vehicle._id;
item.vehicleModel = vehicle.model;
}
results.push(item)
console.log(item)
});
});
resolve(results);
}
})
});
}
Because you use an async function in the map function wish is synchronous you need to create an array of promise and use Promise.all before the resolve to wait for all the results.
The code bellow should fix your issue.
function getBlockUsers() {
return new Promise(function (resolve, reject) {
BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,result) {
if(err){
reject({"msg":"failed to getting block user."})
}else{
var results = result.map(function(item){
// don't forget to return in the map function
return new Promise(function (resolve1, reject1) {
Vehicle.findOne({userId:item.blockedId}).lean().exec(function(err,vehicle){
if (err) return reject1(err)
if(vehicle) {
item.vehicleId = vehicle._id;
item.vehicleModel = vehicle.model;
}
resolve1(item)
});
})
});
// here you wait all the promises of results
resolve(Promise.all(results));
}
})
});
}
The problem is you have non-blocking code inside your result.map().
You should try using just one DB query. Then resolve all the items in the exec callback. Otherwise use a promise for the original query.
Vehicle.find({ $in: { userId: result.map( item => item.blockedId) }}).lean().exec( (err, results) => {
// add the vehicle / model ids to each item in results
resolve(results)
})
Related
I have a function named looper2(), which is accepting an array of data, and im trying to get the id of each data from mysql database.All is working fine, but my nodejs is not waiting for the function, when its still represent await.
var noloop = [];
noloop = await looper2(json_array1);
console.log("db id returns",noloop)
console.log("no loop",noloop[0]); //im getting this undefined
function looper2(){
return new Promise(function(resolve, reject) {
// Do async job
for(var i=0;i<json_array1.length;i++){
var sl = "select id from json where name ="+db.escape(json_array1[i]);
db.query(sl, function(err,result) {
if (err) throw err;
console.log("individual id:", result)
noloop.push(result)
});
}
resolve(noloop)
});
}
The problem is that in looper2 function you resolve immediately after the forLoop and don't wait for the db queries to complete. You should instead resolve after the db queries have all completed
function looper2(){
return new Promise(function(resolve, reject) {
// Do async job
for(var i=0;i<json_array1.length;i++){
var sl = "select id from json where name ="+db.escape(json_array1[i]);
db.query(sl, function(err,result) {
if (err) throw err;
console.log("individual id:", result)
noloop.push(result)
if(noloop.length == json_array1.length) {
resolve(noloop)
}
});
}
});
}
You can't use await without async function. So, you have to create a self-executable function to use await like below. And if you are using promise inside a loop there is promise.all function available, you can use to resolve all pending promises.
(async () => {
var noloop = [];
noloop = await looper2(json_array1);
console.log(noloop);
})();
function async looper2(){
const promises = [];
for(var i=0;i<json_array1.length;i++){
var sl = "select id from json where name ="+db.escape(json_array1[i]);
db.query(sl, function(err,result) {
if (err) throw err;
promises.push(new Promise(result));
});
}
return Promise.all(promises)
}
I am trying to return Array of tokens stored in Firebase, and I am using 'promise'.
function getUsersTokens() {
let dbRef = db.ref('/system/users');
let result = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
let tokens = [];
snap.forEach(child => {
if(child.Status != "occupied"){
helper.getToken(child.key,db).then(function(token){
tokens.push(token);
});
}
});
resolve(tokens);
}, (err) => {
reject(err);
});
});
return result;
}
and this is the 'getToken' method from the "helper" module.
exports.getToken=function(uid,db){
return db.ref(`/Tokens/${uid}`).once('value').then(function(result){
return result.val();
});
};
The problem is that every time I push token into the array it all works fine, but when exit getUsersTokens() the array gets empty.
thanks for the help.
The issue is that your result promise is resolving too early because the helper.getToken() is non-blocking, so your forEach will finish running before all of the getToken() calls have finished pushing their token into tokens.
To make things a little easier, you can split your result promise into two promises. The first promise will be in charge of getting snap. The second promise will be in charge of iterating through snap to produce an array of tokens:
function getUsersTokens() {
let dbRef = db.ref('/system/users');
let result = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
resolve(snap);
}, (err) => {
reject(err);
});
});
return result.then(snap => {
let prommiseArr = [];
snap.forEach(child => {
if(child.Status != "occupied"){
let p = helper.getToken(child.key,db);
promiseArr.push(p);
}
});
return Promise.all(promiseArr); // resolves to array of tokens
});
}
Promise.all takes in an array of promises, and resolves when all of those promises have also resolved. the promise returned by getUsersToken will ultimately contain an array of tokens, because each promise of promiseArr resolves to a token.
It happens because the promise is resolved with the token array before getToken() resolves itself. You see an empty array because your handler runs before the tokens arrive.
You need to wait on that before resolving. Like this:
function getUsersTokens() {
let dbRef = db.ref('/system/users');
return new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
const tokensPromise = snap
.filter(child => child.Status !== "occupied")
.map(child => helper.getToken(child.key, db));
resolve(Promise.all(tokensPromise));
});
});
}
Promise.all as pointed out by #André Werlang and #Christian Santos it perfect here is an example using reduce way
function getUsersTokens() {
let dbRef = db.ref('/system/users');
let result = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
snap.reduce((chain, child) => {
return chain.then(array => {
return helper.getToken(child.key,db).then(function(token){
return array.push(token);
});
});
}, Promise.resolve([])).then(tokens=>{
resolve(tokens);
});
}, (err) => {
reject(err);
});
});
return result;
}
I'm new to NodeJS and after spending a few hours trying to understand how Promises work exactly, what seems to be an easy thing still doesn't work.
I'm trying to make a few calls to a database, and once all of those calls are done, do something else. What I have now is the following code, but none of the then-functions are called.
var queries = ['SELECT value FROM tag', 'SELECT url FROM download_link'];
console.log("Starting promises");
var allPromise = Promise.all([queryDatabaseAddToResult(connection, queries[0], result), queryDatabaseAddToResult(connection, queries[1], result)]);
allPromise.then(
function(result) {
console.log("1"); // Does not show up
}, function(err) {
console.log("2"); // Does not show up either
}
);
function queryDatabaseAddToResult(connection, query, result) {
return new Promise(function(resolve, reject) {
connection.query(query, function(err, rows, fields) {
if (err) {
console.log(err);
Promise.reject(err);
}
console.log(rows);
result.tags = JSON.stringify(rows);
Promise.resolve(result);
});
})
}
The calls to the database do get made, as logging the rows show up in the log.
The problem is that you are not calling the correct resolve and reject functions. It should be:
function queryDatabaseAddToResult(connection, query, result) {
return new Promise(function(resolve, reject) {
connection.query(query, function(err, rows, fields) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(rows);
result.tags = JSON.stringify(rows);
resolve(result);
}
});
})
Note that the resolve and reject calls should not be scoped with Promise.. And you should have used an else to avoid calling resolve once you've called reject.
you have to do like this:
var promise1 = queryDatabaseAddToResult(connection, queries[0], result);
var promise2 = queryDatabaseAddToResult(connection, queries[1],result);
Promise.all([prromise1, promise2]).then(result => {
console.log(1);
}).catch(err => {
console.log(err);
});
return Promise.reject() //no need to add else part
I'm attempting to use nightmarejs to scrape information from a few websites. The problem that I'm running into is, I only want to open one window at a time and wait for it to close before processing the next url.
// index.js
var urls = // an array of urls.
var l = 10; // urls.length;
while (l--) {
// g.findById(id).then()....
// y.findById(id).then()....
UrlProcessing.findById(id).then(function(results) {
console.log(results);
});
}
Now the findByid:
//UrlProcessing.js
class UrlProcessing {
findById(id) {
var address = id;
return new Promise(function (resolve, reject) {
vo(function*(address) {
var nightmare = new Nightmare({show: true});
var link = yield nightmare
.goto(address)
.wait(2000)
.evaluate(function() {
return document.getElementsByTagName('html')[0].innerHTML;
});
yield nightmare.end();
return yield link;
})(address, function(err, res) {
if(err) reject(err);
resolve(res);
});
});
}
module.exports = UrlProcessing;
}
Any suggestions on how I can achieve this? I want to perform each findById from within the while loop.
Without modifying findById, you can simulate series- or waterfall-like behavior using reduce:
var urls = ['http://www.yahoo.com', 'http://example.com', 'http://w3c.org'];
urls.reduce(function(accumulator, url) {
return accumulator.then(function(results) {
return findById(url)
.then(function(result) {
results.push(result);
return results;
});
});
}, Promise.resolve([])).then(function(results){
//do what you need to do with the results
});
For completeness' sake, and because I had to make a couple of touchups, the findById method with my (slight) modifications:
function findById(address) {
return new Promise(function(resolve, reject) {
vo(function * (address) {
var nightmare = new Nightmare({
show: true
});
var link = yield nightmare
.goto(address)
.wait(2000)
.evaluate(function() {
return document.getElementsByTagName('html')[0].innerHTML;
});
yield nightmare.end();
return link;
})(address, function(err, res) {
if (err) reject(err);
resolve(res);
});
});
}
... all that being said, I'm not sure this approach is best. Why do you want them one at at a time in separate Nightmare instances? I realize this doesn't totally fit your original implementation, but this may be something you want to consider - you could change findById around to accept an array instead of a single URL and also (optionally) use the same Nightmare instance. Calling findById:
var urls = ['http://www.yahoo.com', 'http://example.com', 'http://w3c.org'];
findById(urls)
.then(function(results) {
//do what you need to do with the results
});
... and findById itself:
function findById(addresses) {
return new Promise(function(resolve, reject) {
vo(function * (addresses) {
var nightmare = new Nightmare({
show: true
});
var results = [];
for (var i = 0; i < addresses.length; i++) {
results.push(yield nightmare
.goto(addresses[i])
.wait(2000)
.evaluate(function() {
return document.getElementsByTagName('html')[0].innerHTML;
}));
}
yield nightmare.end();
return results;
})(addresses, function(err, res) {
if (err) reject(err);
resolve(res);
});
});
}
Of course, if you still wanted fresh Nightmare instances every time, you could move the constructor call and the call to .end() inside of the for loop.
You are doing everything almost correct, now all you need to do is, sequentialize the promises i.e chain them, you can take a look at this answer.
Just change your code to use reduce:
// index.js
urls.reduce( function(promise, url){
return promise.then(function(){
return url.findById(id);
}).then(function(results){
console.log(results);
});
}, Promise.resolve())
.then(function(){
console.log('All done');
});
in more condensed ES6 form, it would be:
urls.reduce( (p, url) => p.then(() => url.findById(id)).then(r => console.log(r)), Promise.resolve())
.then(() => console.log('All done') );
It's a little complicated. Let show the code:
requestAccessToken().then(function(requestResult) { // this is the first then()
if(requestResult.ok == 1) {
return document;
}
}).then(function(document) { // this is the second then()
return db.update(updateArgu); // here, db.update will return a promise obj
}).then(function(result) { // this is the last then()
console.log('update ok');
console.log(result);
});
Since the db.update(updateArgu) will return a promise object, it can add a .then() method like db.update().then().
But I want to keep the main chain like requestAccessToken().then().then() so I returned the db.update() in the second then(). The output is:
app-0 update ok
app-0 undefined
The db.update code is:
exports.update = function(arguments) {
var name = arguments.name;
var selector = (arguments.selector) ? arguments.selector : {};
var document = (arguments.document) ? arguments.document : {};
var options = (arguments.options) ? arguments.options : {};
return Promise(resolve, reject) {
MongoClient.connect(DBURL, function(err, db) {
if(err) throw err;
db.collection(name).update(selector, document, options, function(err, result) {
db.close();
if(err) {
reject(err);
} else {
resolve(result);
}
});
});
}
}
You can see it has resolve(result), how can I transfer it to the last then()?
I'll make my comment into an answer.
Where you do:
return Promise(resolve, reject) {
...
}
it should be:
return new Promise(function(resolve, reject) {
...
});