mongodb - Node.js, Mongo find and return data -
i’m new node , mongo after 15 years of vb6 , mysql. i’m sure not final program use need basic understanding of how call function in module , results back.
i want module have function open db, find in collection , return results. may want add couple more functions in module other collections too. need simple possible, can add error handlers, etc later. been on days trying different methods, module.exports={… around function , out it, .send, return no luck. understand it’s async program may have passed display point before data there.
here’s i’ve tried mongo running database of db1 collection of col1.
db1.js var mongoclient = require('mongodb').mongoclient; module.exports = { findincol1 : function funk1(req, res) { mongoclient.connect("mongodb://localhost:27017/db1", function (err,db) { if (err) { return console.dir(err); } var collection = db.collection('col1'); collection.find().toarray(function (err, items) { console.log(items); // res.send(items); } ); }); } }; app.js a=require('./db1'); b=a.findincol1(); console.log(b); console.log(items) works when 'findincol1' calls not console.log(b)(returns 'undefined') i'm not getting return or i'm pasted time returns. i’ve read dozens of post , watched dozens of videos i'm still stuck @ point. appreciated.
as mentioned in answer, code asynchronous, can't return value want down chain of callbacks (nested functions). need expose interface lets signal calling code once have value desired (hence, calling them back, or callback).
there callback example provided in answer, there alternative option worth exploring: promises.
instead of callback function call desired results, module returns promise can enter 2 states, fulfilled or rejected. calling code waits promise enter 1 of these 2 states, appropriate function being called when does. module triggers state change resolveing or rejecting. anyways, here example using promises:
db1.js:
// db1.js var mongoclient = require('mongodb').mongoclient; /* node.js has native support promises in recent versions. if using older version there several libraries available: bluebird, rsvp, q. i'll use rsvp here i'm familiar it. */ var promise = require('rsvp').promise; module.exports = { findincol1: function() { return new promise(function(resolve, reject) { mongoclient.connect('mongodb://localhost:27017/db1', function(err, db) { if (err) { reject(err); } else { resolve(db); } } }).then(function(db) { return new promise(function(resolve, reject) { var collection = db.collection('col1'); collection.find().toarray(function(err, items) { if (err) { reject(err); } else { console.log(items); resolve(items); } }); }); }); } }; // app.js var db = require('./db1'); db.findincol1().then(function(items) { console.info('the promise fulfilled items!', items); }, function(err) { console.error('the promise rejected', err, err.stack); }); now, more date versions of node.js mongodb driver have native support promises, don't have work wrap callbacks in promises above. better example if using date driver:
// db1.js var mongoclient = require('mongodb').mongoclient; module.exports = { findincol1: function() { return mongoclient.connect('mongodb://localhost:27017/db1').then(function(db) { var collection = db.collection('col1'); return collection.find().toarray(); }).then(function(items) { console.log(items); return items; }); } }; // app.js var db = require('./db1'); db.findincol1().then(function(items) { console.info('the promise fulfilled items!', items); }, function(err) { console.error('the promise rejected', err, err.stack); }); promises provide excellent method asynchronous control flow, highly recommend spending time familiarizing them.
Comments
Post a Comment