junosuarez/objectid

Casting between this and native mongodb driver ObjectIds

junosuarez opened this issue · 0 comments

via @kurttheviking:

a contrived but simple example:

var mongodb = require('mongodb');
var objectid = require('objectid');
var notNativeId = objectid();
mongodb.ObjectID(notNativeId);

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

From my previous analysis it comes down to the fact that when the input is cast into a native oid, the underlying bson library first runs an isValid check. The argument is not a number or a string (typeof notNativeId === 'object') so it immediately checks the length. notNativeId has no length property so it fails validation (notNativeId.length === undefined where 12 or 24 was expected.)