findOne NodeJS MongoDB 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19625827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:06:29  来源:igfitidea点击:

findOne NodeJS MongoDB driver

node.jsmongodb

提问by Leahcim

I have JSON data like you see below in a collection called 'English', for which I'm setting up a REST api with a nodejs app using the MongoDB driver. If I do the following, I get all the JSON data returned in the browser.

我有 JSON 数据,就像您在下面一个名为“英语”的集合中看到的那样,我正在使用 MongoDB 驱动程序设置带有 nodejs 应用程序的 REST api。如果我执行以下操作,我将获得浏览器中返回的所有 JSON 数据。

app.get('/sentences', function (req, res){

     db.collection('english', function(err, collection) {
        collection.find().toArray(function(err, items) {

            res.send(items);
        });
    });

})

However, when I try to go to /sentences/1to get one record, the app is freezing (the half-moon in the browser tab turns slowly) and there's no error logged. Is there something wrong with how I'm doing this findOne?

但是,当我尝试去/sentences/1获取一条记录时,该应用程序冻结(浏览器选项卡中的半月转得很慢)并且没有记录错误。我做这个 findOne 的方式有什么问题吗?

app.get('/sentences/:id', function(req,rest){

     var query = { 'question' : req.params.id };
     console.log(query);
     console.log('query');

    db.collection('english', function(err, collection) {

        collection.findOne(query, function(err, item) {
            console.log(err);
            res.send(item);
        });
    });
});

JSON data

JSON 数据

[
  {
    "_id": "526c0e21977a67d6966dc763",
    "question": "1",
    "uk": "blah blah blah",
    "us": "blah blah balh"
  },
  {
    "_id": "526c0e21977a67d6966dc764",
    "question": "2",
    "uk": "Tom went outside for a fag. I think he smokes too much!",
    "us": "Tom went outside for a cigarette. I think he smokes too much!"
  },
  {
    "_id": "526c0e21977a67d6966dc765",
    "question": "3",
    "uk": "Do you fancy going to the cinema on Friday?",
    "us": "How about going to the movies on Friday"
  }
]

Update

更新

What's happening is that the app is eventually timing out and I get a No data receivedmessage in the browser.

发生的事情是应用程序最终超时,我No data received在浏览器中收到一条消息。

回答by Muhammad Shahzad

nodejs mongodb findOne by id

nodejs mongodb findOne by id

It is late but will be helpful others.

为时已晚,但会对其他人有所帮助。

var id = new require('mongodb').ObjectID('58fcf02b1ab5de07e2a1aecb');//req.params.id
db.collection('users').findOne({'_id':id})
 .then(function(doc) {
        if(!doc)
            throw new Error('No record found.');
      console.log(doc);//else case
  });

回答by Leahcim

The problem was a typo in one of the arguments (an extra 't' in 'res'). Instead of

问题是其中一个参数的拼写错误('res' 中的一个额外的 't')。代替

app.get('/sentences/:id', function(req,rest){
...

res.send(item);

should have been

本来应该

app.get('/sentences/:id', function(req,res){ 
...

res.send(item);

回答by Mohamed Ali

It is not good but still will be helpful others.

这不好,但仍然会对其他人有所帮助。

var id = new require('mongodb').ObjectID('58fcf02b1ab5de07e2a1aecb');//req.params.id
db.collection('users').findOne({'_id':id})
 .then(function(doc) {
        if(!doc)
            throw new Error('No record found.');
      console.log(doc);//else case
  });