NodeJS Error: object not a valid ObjectId

290 Views Asked by At

I have a game server (alt:V) and I want to update the database with the current state of the character. My problem is that this code:

console.log(`Character id at disconnect: ${id}`)
let saved = await db.updatePartialData(id, player.data, 'characters')

Returns the error:

Error: object [{"_bsontype":"ObjectID","id":{"0":95,"1":149,"2":92,"3":176,"4":81,"5":130,"6":202,"7":111,"8":255,"9":155,"10":50,"11":164}}] is not a valid ObjectId
    at serializeObjectId (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:287:11)
    at serializeInto (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:935:17)
    at serializeObject (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:941:17)
    at serializeObject (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/simon/altv-server/node_modules/bson/lib/bson/parser/serializer.js:941:17)
    at BSON.serialize (/home/simon/altv-server/node_modules/bson/lib/bson/bson.js:64:28)
    at Msg.serializeBson (/home/simon/altv-server/node_modules/mongodb/lib/core/connection/msg.js:126:22)
    at Msg.makeDocumentSegment (/home/simon/altv-server/node_modules/mongodb/lib/core/connection/msg.js:118:33)
    at Msg.toBin (/home/simon/altv-server/node_modules/mongodb/lib/core/connection/msg.js:104:25)

Although the first line prints this to the console:

Character id at disconnect: 5f955cb05182ca6fff9b32a4

The updatePartialData function is part of the simplymongo npm package and defined as follows:

import mongodb from 'mongodb';
export const ObjectID = mongodb.ObjectID;

...

async updatePartialData(id, partialObjectData, collection) {
        try {
            await this.db
                .collection(collection)
                .findOneAndUpdate({ _id: ObjectID(id) }, { $set: { ...partialObjectData } });

            return true;
        } catch (err) {
            console.error(err);
            return false;
        }
}

I am running this on Debian 10, NodeJS 15.0.1 and all of my npm packages are up to date. Please help if you can. This is driving me crazy.

0

There are 0 best solutions below