简体   繁体   English

db.collection.find() 在 node.js 中返回空数组,但在 mongo shell 中正常工作

[英]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".所以基本上我试图通过查询属性“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.我从 url 获得“uid”,然后基于它我想呈现不同的视图并从 find() function 传递文档,但它只是返回一个空数组。 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.正如我通过 console.log 语句确认的那样,“uid”正在从 url 正确获取,但是当我 console.log 由 find() 生成的文档时,它是空的。 So confused because when I run the command in the mongo shell it returns the correct document.很困惑,因为当我在 mongo shell 中运行命令时,它会返回正确的文档。

/*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.我认为 db.get 将返回 promise 所以你需要等待 db.get 语句或者你应该使用.then 来捕获响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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