Node.js mongodb cursor.each get empty, can't run in sequence -


server.get('/getlist', function (req, res, next) {     db.collection('lists',function(error, collection) {         var gets = [];         var cursor = collection.find({status: 1});         var = 0;         cursor.each(function (err, docs) {             console.log(docs);             gets[i] = docs;             = + 1;         });         res.send(gets);     });      return next(); }); 

it can log out correctly, website "http://127.0.0.1:8080/getlist" result empty "[]". how make cursor.each run in sequence every source code line? or there better solution?

you use toarray convert cursor array:

cursor.toarray(function(err,documents){    res.send(documents);    next(); }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -