I would like to understand why this:
req.session._id = doc._id;
req.session.id = doc._id;
console.log(typeof req.session._id); // object
console.log(typeof req.session.id); // string
console.log(req.session._id); // 4oqTbA06DcK9cIiU3tnK62Ic.0puZ9uPnIaghMBIQKaIbhIHYzeP8wtS3MXnzpi+yC1c
console.log(req.session.id); // 4fb01ad2ca42e9552d000001
So, when i want to do this:
var myID = req.session._id, myIDm = new BSON.ObjectID(myID); // don't bug
var myID = req.session.id, myIDm = new BSON.ObjectID(myID); // bug: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
Thank you for tell me all information about this!
The problem is, _id
is autogenerated by mongodb and is of type ObjectID
which can be passed to BSON.ObjectID()
and you get new ObjectId
with the same id. The id
attribute is not managed by mongodb and if you put this value to BSON.ObjectID()
constructor, it raises error. The expected string must be 12 bytes or hex encoded binary of length 24 (allowed chars are a-f, A-F, 0-9). Hope this helps.