开发者

Node.js / mongoDB, cannot retrieve records

开发者 https://www.devze.com 2023-02-23 03:32 出处:网络
In my node.js / express app, when I look into mongoDB, I got the following: > db.things.find()[0]._id

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){

I开发者_如何学运维t 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).


The problem was in the callback function where I should have used (err, thing) as parameters instead of only (thing).


Try:

Thing.findById(id, function(thing){
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号