findOne NodeJS MongoDB驱动程序

我有一个JSON数据,就像你在下面看到的一个名为“English”的集合,为此我使用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); }); }); }) 

但是,当我尝试去/sentences/1来获得一个logging,应用程序是冻结(浏览器选项卡中的半月缓慢),并没有logging错误。 我在做这个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数据

 [ { "_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" } ] 

更新

发生什么事情是该应用程序最终超时,并在浏览器中No data received消息。

nodejs mongodb findOne by id

已经晚了,但会帮助别人。

 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 }); 

问题是其中一个论点的错字('res'中的额外't')。 代替

 app.get('/sentences/:id', function(req,rest){ ... res.send(item); 

本来应该

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