简体   繁体   中英

db.collection.find() returning empty array in node.js but works properly in mongo shell

So essentially I'm trying to find a specific document by querying the attribute "uid". I get the "uid" from the url and then based on that I want to render a different view and pass the document from the find() function but it's just returning an empty array. The "uid" is being grabbed correctly from the url as I confirmed with a console.log statement but when I console.log the doc that results from find() it's empty. So confused because when I run the command in the mongo shell it returns the correct document.

/*GET UserDetails page.*/ 
router.get('/userdetails', function(req, res) {
  var queryID = req.query.uid;
  //console.log(queryID);
  var db = req.db;
  var collection = db.get('usercollection');
  collection.find({"uid" : queryID},{},function(e,docs){
      //console.log(docs);
      res.render('userdetails', {
          "userinfo" : docs
      });
  });
});

Any help would be greatly appreciated!

I think db.get will return the promise so u need to await for db.get statement or u should use.then to capture the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM