NodeJS & Mongoskin, cannot perform a simple upgrade. The argument passed in must be 12 bytes or 24 hexadecimal strings

Using mongoskin .

I am trying to do a simple update and I keep getting the error:

Error: the argument passed in must be a single String of 12 bytes or a string of 24 hexadecimal characters in hexadecimal format

Different code I tried:

var mongo = require('mongoskin'), store = mongo.db(MONGO_DB_ADDESS + ':' + MONGO_DB_PORT + '/' + MONGO_DB_NAME + '?auto_reconnect=false'); session._id = 4eb5444d39e153e60b000001; store.collection('sessions').updateById({_id : session._id}, {$set: status_obj}, {upsert : false, multi : false, safe : false}, function() { ... }); store.collection('sessions').updateById(session._id, {$set: status_obj} ); 

I even tried:

 store.collection('sessions').update( {'_id': session._id}, {$set: {"status":'unavailable'}} ); 

Any help appreciated!

Thanks Fyi, I can do the update via mongo using cli just fine:

 db.sessions.update( {'_id': ObjectId('4eb5444d39e153e60b000001')}, {$set: {"status":'unavailable'}} ); 
+6
source share
4 answers
 store.collection('sessions').updateById(session._id.toString(), {$set: status_obj} ); 

Adding .toString () finally resolved this for me.

+17
source

A similar error occurred with Mongoose, it turned out that my identifier was erroneous, but perhaps this verification function may help you:

 function validate_id(id) { return !(id !== null && (id.length != 12 && id.length != 24)); } 
+3
source

put this at the root of your javascript

ObjectID = require('mongoskin').ObjectID;

collection.updateById(new ObjectID(song._id), <json>, <callback>);

puts _id, you get to node in the format mongo requires

+1
source

You can also try using trim() to remove spaces before or after the line, which will result in the same original error:

 store.collection('sessions').updateById(session._id.toString().trim(), {$set: status_obj} ); 
0
source

Source: https://habr.com/ru/post/900848/


All Articles