Node.js / mongoDB,无法检索记录
在我的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();
});
没有返回任何内容。
有什么想法吗?
* UPDATE *
这也不起作用:
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)...
* SOLUTION *
我的错......
问题不在于 find 函数,而在于回调我使用的方法...我只提供了一个我应该提供的参数(东西)(err,tring)。
In my node.js / express app, when I look into mongoDB, I got the following:
> db.things.find()[0]._id
ObjectId("4da06702584ca3f402000001")
It does exists a collection with id 4da06702584ca3f402000001. But When I use a request, I cannot get it back:
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();
});
Nothing is returned.
Any idea ?
* UPDATE *
This does not work either:
Thing.find({ _id:ObjectId("4da06702584ca3f402000001")}, function(thing){
It raises the following error:
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)...
* SOLUTION *
My bad....
The problem is not with the find function but with the callback method I use... I only provided one single parameter (thing) where I should provide (err, tring).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在回调函数中,我应该使用 (err, thing) 作为参数,而不是仅使用 (thing)。
The problem was in the callback function where I should have used (err, thing) as parameters instead of only (thing).
尝试:
Try: