0

I have a collection with an ID (_ID).

{
  "_id": ObjectId("5583df9da55f654609901eed"),
  "group": "Tomorrowland",
  "description": "Délire 2015 à Tomorrowland",
  "uid": [
    "5583df21a55f654609901eec",
    "5583e8ef9aeb31390bb50bf6"
  ],
  "pictures": [
    "1434705960113.jpg",
    "1434705974710.jpg",
    "1434706147177.jpg",
    "1434711007201.jpg"
  ],
  "__v": 0
}

If a make a query (FindByID) and I check for the ID 1 i get an error (all work fine in this case : the id not exist)

Now if i make the same query and I check for the ID (5582a7f12686cf776a0daee5), I get no error ... but the ID doen't exist in my collection !

How is it possible ?

Here is the code :

groupid = req.headers['groupid'];

Cloud.findById(groupid).exec(function(err, data)
{
  if(err){
    res.status(404);
    res.json('group not found');
  }
  else
  {
    // Do Stuff
  }
});
Pixelight
  • 63
  • 1
  • 2
  • 12
  • Well does it return anything as a result? No? ( rhetorical ). So you asked for nothing and it returned nothing. Why should this be an error? Errors are reserved for network errors. Would you expect `select null from table` to return an error in SQL RDBMS? This is what you should be thinking about. –  Jun 19 '15 at 12:48

1 Answers1

0

You need to convert the string _id to a valid Mongodb ID

var mongo = require('mongodb'),
    BSON = mongo.BSONPure,
    groupid = new BSON.ObjectID(req.headers['groupid']);

Cloud.find({'_id': groupid}).exec(function(err, data)
{
  if(err){
    res.status(404);
    res.json('group not found');
  }
  else
  {
    // Do Stuff
  }
});

If you use Mongoose you can convert the string to _id as:

var mongoose = require('mongoose'),
    groupid = mongoose.Types.ObjectId(req.headers['groupid']);
michelem
  • 14,430
  • 5
  • 50
  • 66