Skip to content

Casting between this and native mongodb driver ObjectIds #4

@junosuarez

Description

@junosuarez

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions