node.js - Retrieving value of count mongodb collection in a variable -
i trying retrieve value of records collection in mongo db
here source.
exports.procc = function(req, res) { db.collection('search', function(err, collection) { collection.count({'str':'car'},function(err, count) { console.log(count+' records');//prints 2 records c=count; }); }); console.log('records= '+c);//print 0 records res.end(); }; the problem out of callback prints number of register, out of callback prints 0 , don't know how save value in variable.
because db.collection , collection.count asynchronous methods, c variable getting set after second console.log statement executed.
so want c has occur within collection.count callback.
Comments
Post a Comment