Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument passed in must be a single String of 12 bytes

mongoDB collection contains the following data

db.stack.find()
{ "_id" : "8GieRu" }

The _id is not single String of 12 bytes,

As per the MongoDB document of [ObjectID][1], id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.

Using Mongoose this collection is accessed using this Json

{"_id" : new mongoose.Types.ObjectId("8GieRu")}

and throws the below error

/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35
    throw new Error("Argument passed in must be a single String of 12 bytes or
          ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35:11)

  [1]: http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html

Mongoose is strictly checking the ObjectId of fixed length, how can i pass Object_id using mongoose with the given length

like image 668
anish Avatar asked Oct 19 '14 17:10

anish


4 Answers

You mix two concepts here.

While "_id" can have any value (even subdocument like {firstName:'Foo',lastName:'Simpson'}, "ObjectId" has a fixed set of types with some restrictions, as the error message correctly states.

So your statement should be

{'_id':'putWhatEverYouWantHere'}
like image 52
Markus W Mahlberg Avatar answered Oct 24 '22 04:10

Markus W Mahlberg


I had the problem in my router order:

app.get('/jobs', controllers.jobs.getAllJobs);
app.get('/jobs/karriere', controllers.jobs.getAllJobsXML);

app.get('/jobs/:id', controllers.jobs.getJob);
app.get('/jobs/:id/xml', controllers.jobs.getJobXML);

I defined /jobs/karriere after /jobs/:id so the application thought that "karriere" was an ObjectID and returned the error. The code above is the working one.

like image 36
mrks Avatar answered Oct 24 '22 04:10

mrks


Make sure the method you are using in client and server side match. This error also shows when you have e.g. GET being sent from client side and POST required on the server side.

like image 28
Tashfeen Avatar answered Oct 24 '22 04:10

Tashfeen


You are passing any

ObjectID undefinded 

If the ObjectID is undfined thaen this error will come.

like image 41
sibaspage Avatar answered Oct 24 '22 04:10

sibaspage