[英]Node.js / mongoDB, cannot retrieve records
在我的node.js / express应用程序中,当我查看mongoDB时,得到以下信息:
> db.things.find()[0]._id
ObjectId("4da06702584ca3f402000001")
它确实存在一个ID为4da06702584ca3f402000001的集合。 但是,当我使用一个请求时,我无法将其取回:
app.get('/thing/show', function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
Thing = mongoose.model('Thing');
id = "4da06702584ca3f402000001";
Thing.find({ _id : id }, function(thing){
console.log("THING RETRIEVED:" + thing);
res.write(JSON.stringify(thing));
});
res.end();
});
什么也没退。
任何想法 ?
*更新*
这也不起作用:
Thing.find({ _id:ObjectId("4da06702584ca3f402000001")}, function(thing){
它引发以下错误:
Error: This is an abstract interface. Its only purpose is to mark fields as ObjectId in the schema creation.
at ObjectId (/usr/local/lib/node/.npm/mongoose/1.1.24/package/lib/mongoose/schema.js:426:9)...
*解决方案*
我的错....
问题不在于find函数,而是我使用的回调方法...我只提供了一个我应该提供(err,tring)的参数(东西)。
问题出在回调函数中,我应该使用(err,thing)作为参数,而不仅仅是(thing)。
尝试:
Thing.findById(id, function(thing){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.