Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS & Mongoskin, can't do simple update. Argument passed in must be 12 bytes or 24 hex string

Using mongoskin.

I'm trying to do a simple update and I keep on getting the error:

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format

Different code that I have 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} );      

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 the cli just fine:

db.sessions.update( {'_id': ObjectId('4eb5444d39e153e60b000001')}, {$set: {"status":'unavailable'}} );
like image 772
PaulM Avatar asked Nov 05 '11 16:11

PaulM


People also ask

What is NodeJS used for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Is NodeJS a programming language?

Node. js is not a programming language. Rather, it's a runtime environment that's used to run JavaScript outside the browser.

Is NodeJS frontend or backend?

Node. js is sometimes misunderstood by developers as a backend framework that is exclusively used to construct servers. This is not the case; Node. js can be used on the frontend as well as the backend.

Is NodeJS better than Java?

Java uses the concept of multithreading with ease, whereas Node JS does not use the concept of multi-threading like Java does. For large scale projects that involved concurrency, Java is highly recommended, whereas Node JS does not handle the thread and Java, which is the weakest point of this framework.


3 Answers

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

Adding .toString() finally resolved this for me.

like image 89
PaulM Avatar answered Oct 08 '22 14:10

PaulM


Did have a similar error with Mongoose, it turned out my id was wrong, but maybe this validation function can help your:

function validate_id(id) {
  return !(id !== null && (id.length != 12 && id.length != 24));
}
like image 23
alessioalex Avatar answered Oct 08 '22 14:10

alessioalex


put this in the root of your javascript

ObjectID = require('mongoskin').ObjectID;

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

puts the _id you get in node into the format mongo needs

like image 30
Dan But Avatar answered Oct 08 '22 16:10

Dan But