-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 6.3.0
-
Component/s: BSON
If I import ObjectId from the bson package and compare those instances against the ones created by ObjectId from the mongodb package, an exception is thrown, as follows:
node_modules/bson/lib/bson.mjs:2118 return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]); ^TypeError: Cannot read properties of undefined (reading '11') at ObjectId.equals (file:node_modules/bson/lib/bson.mjs:2118:50) at file:///dist/ttt.js:9:4
The set up is an internal package that implements all interactions with Mongo DB and the application package that passes ObjectId instances into methods of the internal package. The intent here is that the application does not need or should access the database directly and only uses specific types, like ObjectId.
This is npm output for both, bson and mongodb.
npm list bson app@0.19.0 +-- bson@6.2.0 `-- internal-package@0.8.0 `-- mongodb@6.3.0 `-- bson@6.2.0 deduped npm list mongodb app@0.19.0 `-- internal-package@0.8.0 `-- mongodb@6.3.0
Here's the code to reproduce an error. It's TypeScript that is built with node16 modules, for NodeJS v16.20.2.
import { ObjectId } from "mongodb"; import { ObjectId as BsonObjId} from "bson"; let o1: BsonObjId = BsonObjId.createFromHexString("61c4a0a1033ad3de068c5854"); let o2: ObjectId = ObjectId.createFromHexString("61c4a0a1033ad3de068c5854"); let o3: BsonObjId = BsonObjId.createFromHexString("61c4a0a1033ad3de068c5854"); let o4: ObjectId = ObjectId.createFromHexString("61c4a0a1033ad3de068c5854"); // works o1.equals(o3); o2.equals(o4); // throws o1.equals(o2); o2.equals(o3);
The problem appears to be in otherId[kId] always being undefined, perhaps because kId is mixed in somehow between modules.
This used to work for years and got broken for me when I updated Mongo DB to the version listed above.
- is duplicated by
-
NODE-5796 Different instanceof results for commonjs and esmodule
- Closed
- is fixed by
-
NODE-5873 js-bson - PR #643: fix: use Symbol.for('id') in ObjectId to fix errors when mixing cjs and mjs
- Closed
- is related to
-
NODE-6443 instanceof checks don't work when mongodb and bson are used together in ESM project
- Blocked
-
NODE-5872 ObjectId.equals throws vague error two bson packages are loaded
- Closed
-
NODE-5873 js-bson - PR #643: fix: use Symbol.for('id') in ObjectId to fix errors when mixing cjs and mjs
- Closed