Not able to run "find" query in remote mongodb collection using MongoClient in Node.js application -
var express = require("express"); var bodyparser = require('body-parser'); var path = require("path"); var objectid = require('mongodb').objectid; var mongoclient = require('mongodb').mongoclient, format = require('util').format; var uri = 'mongodb://ac-00072810.devapollogrp.edu:27017/devdb'; mongoclient.connect(uri,function(err,db){ db.collection('transactionstatus',function(err,collection){ if(err){ throw err; } else{ collection.find({}).foreach(function(err,doc){ console.log(doc); }); } }); });
getting error:
[apol@ac-00067394 nodejs-testapp]$ node server.js body-parser deprecated bodyparser: use individual json/urlencoded middlewares server.js:15:9 body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29 running @ port 3001 undefined
(note: mongodb version:3.0.2,node version:0.12.7, express version:4.12.4)
from mongo shell able details successfully. when trying fetch data using mongoclient form application getting error "undefined". tried possibilities. don't know doing wrong.
thanks in advance :)
at glance looks you're getting [foreach][1]
behaviour wrong. callback passed foreach
accepts 3 params: currentvalue, index , array, respectively, since it's core javascript function , not node-specific (which why followed err
, doc
convention).
try in else
block:
collection.find({}).toarray(function(err, docarray){ console.log(docarray); });
Comments
Post a Comment