Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

EJSON.stringify throw exception when object contains array of ObjectID #1

Closed
avivr opened this issue May 24, 2017 · 0 comments
Closed

Comments

@avivr
Copy link

avivr commented May 24, 2017

const ExtJSONModule = require("mongodb-extjson");
const EJSON = new ExtJSONModule();
const BSON = ExtJSONModule.BSON;
const { ObjectID } = BSON;

const test1 = { _id: { "$nin": [ {"$oid":"591801a468f9e7024b6235ea"} ] } }
const str1 = EJSON.stringify(test1); // this one works
console.log(str1);

const test2 = { _id: { "$nin": [ new ObjectID("591801a468f9e7024b6235ea") ] } };
const str2 = EJSON.stringify(test2); // throw exception
console.log(str2);

It throws the following exception:

/Users/aviv/Documents/tryouts/extjson/node_modules/mongodb-extjson/lib/bson/objectid.js:89
    throw new Error('invalid ObjectId, ObjectId.id must be either a string or a Uint8Array, but is [' + JSON.stringify(this.id) + ']');
    ^

Error: invalid ObjectId, ObjectId.id must be either a string or a Uint8Array, but is [{"0":{"$numberInt":"89"},"1":{"$numberInt":"24"},"2":{"$numberInt":"1"},"3":{"$numberInt":"164"},"4":{"$numberInt":"104"},"5":{"$numberInt":"249"},"6":{"$numberInt":"231"},"7":{"$numberInt":"2"},"8":{"$numberInt":"75"},"9":{"$numberInt":"98"},"10":{"$numberInt":"53"},"11":{"$numberInt":"234"}}]
    at ObjectID.toHexString (/Users/aviv/Documents/tryouts/extjson/node_modules/mongodb-extjson/lib/bson/objectid.js:89:11)
    at ObjectID.toJSON (/Users/aviv/Documents/tryouts/extjson/node_modules/mongodb-extjson/lib/bson/objectid.js:171:23)
    at JSON.stringify (<anonymous>)
    at ExtJSON.stringify (/Users/aviv/Documents/tryouts/extjson/node_modules/mongodb-extjson/lib/ext_json.js:161:15)
    at Object.<anonymous> (/Users/aviv/Documents/tryouts/extjson/index.js:11:20)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant