Argument passed in must be a string of 24 hex characters - I think it is
Asked Answered
V

17

46

I have a method to find a document in my database based on its ObjectID:

      console.log('id: ' + id + ' type: ' + typeof id);
      collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
        if (error) {
          callback(error);
        } else {
           callback(null, doc);
        }
      });

When I run it I get the following error:

/myPath/node_modules/monk/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/base.js:245
    throw message;      
          ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/myPath/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:38:11)
at /myPath/collectionDriver.js:134:41

This refers to the collection.findOne() line above.

The console log I have before that call outputs the id as a string of 24 hex characters:

id: "55153a8014829a865bbf700d" type: string

Before this I convert the id from an object to a string using JSON.stringify() but it appears to work successfully as shown in my console.log.

Running db.myCollection.findOne({_id : ObjectId("55153a8014829a865bbf700d")}) in Robomongo brings back the expected result.

Vaporetto answered 5/5, 2015 at 11:5 Comment(0)
V
41

The id that was passed in to my function was already an object ID in this case, so did not need a new ObjectID to be created from it.

When ObjectIDs are logged out to the console they appear as hex strings, rather than ObjectID("hexString"), so I thought I needed to convert it to do the find, but it was already in the format that I needed.

Vaporetto answered 6/5, 2015 at 8:51 Comment(1)
Thanks for your solution. I'm running into this issue as well, however, if I remove ObjectId(...) I get an Invalid Arg Type Error. If I leave the object Id, it works, but I get this error message every time. Any thoughts on this?Alysa
L
4

Try this:

var hex = /[0-9A-Fa-f]{6}/g;
id = (hex.test(id))? ObjectId(id) : id;
collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
  if (error) {
    callback(error);
  } else {
    callback(null, doc);
  }
});
Linalool answered 8/9, 2016 at 5:14 Comment(0)
R
3

In my case, this worked:

var myId = JSON.parse(req.body.id);
    collection.findOne({'_id': ObjectID(myId)}, function(error,doc) {
    if (error) {
      callback(error);
    } else {
       callback(null, doc);
    }
});

Don't forget to include at the beginning:

var ObjectId = require('mongodb').ObjectID;
Redundant answered 8/11, 2016 at 0:15 Comment(0)
C
2

Yeah, I just spent thirty minutes baffled by this. If anyone finds themselves here, first check if you even need to convert to ObjectID in the first place. Like OP, I forgot that the hexstring is logged as just that - a hexstring.

Crider answered 23/5, 2020 at 2:9 Comment(0)
F
1

Try out ObjectID(id) instead of new ObjectID(id)

Faintheart answered 5/5, 2015 at 11:12 Comment(3)
Thanks for the suggestion, but this does not change the outcome.Vaporetto
I just tried following code nad it recognizing hexstring: var ObjectID = require('mongodb').ObjectID;ObjectID.createFromHexString('55153a8014829a865bbf700d') 55153a8014829a865bbf700dFaintheart
Yeah, works for me like that too, but not with my input, which apparently isn't the hex string it appears to be.Vaporetto
R
1

I got the same error. Later I found that I have included a whitespace while sending the values to the server.

<input type="checkbox" name="checkbox" value="<%= item._id %>" onchange="this.form.submit()" />
  

In the value attribute I had typed as value="<%= item._id %> " . A whitespace after the ID. It caused to that error. After I removed it(value="<%= item._id %>" ), the problem solved.

The below code worked for me.

item.findOne({'_id':id} , (err)=>{

if(err){
  console.log(err);
}
else{
  console.log("Item Found");
}

});

Radioscopy answered 6/2, 2022 at 8:47 Comment(0)
D
1

Actually the Id you are fetching have whitespaces in it so adding trim() to your id will work

const id = (req.params.id).trim();
const result =  await deleteOne({_id:new mongodb.ObjectId(id)});

while using this code keep in mind that your function should be async and mongodb should have been imported.

Delegation answered 11/2, 2022 at 7:24 Comment(0)
C
0

I was having the same issue but trimming the productId passed to ObjectId() worked for me

Earlier:

    const objectId = new ObjectID(productId);
    product = await db?.collection('products').find({ _id: objectId }).next();

Now:

    const objectId = new ObjectID(productId.trim());                                       
    product = await db?.collection('products').find({ _id: objectId }).next();
Conroy answered 23/2, 2021 at 6:20 Comment(0)
P
0

I found the source of this,

make sure the value must not have any whitespace,

you can use const ID = id.trim(); then use ObjectId(ID)

Thanks Happy Coding

Protuberate answered 24/4, 2021 at 18:4 Comment(0)
A
0

Argument passed in must be a string of 24 hex characters if you are getting this error then. just do this

use .trim() to delete any extra character usually a space is added to the id. and it becomes 25 hex charater . so instead of _id = yourId; use _id = yourId.trim()

Aeromechanic answered 6/5, 2021 at 3:44 Comment(0)
E
0

collection.find({ _id: { $in: req.body.map(pd => ObjectId(pd)) } })

use map() on the res.body -- it works for me.

Euphuism answered 19/5, 2021 at 22:26 Comment(0)
G
0
const ID = (req.body.id).trim();

Worked for me, actually (req.body.id) adds an extra space to the ID variable.

After I used .trim() method it worked.

Goodbye answered 17/7, 2021 at 6:10 Comment(0)
C
0

I have tried all answers. But it doesn't work for me. Maybe if you are getting params with express. It's worked for me. Try this;

var MongoClient = require('mongodb').MongoClient;
const {ObjectId} = require('mongodb');

app.get('/:id', (req, res) => {

var reqId = req.params.id
var query = reqId.toString()


MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("AllDB");
    dbo.collection("Events").findOne({_id: ObjectId(query)}, function(err, result) {
        if (err) throw err;
        res.send(result)
        db.close();
      });
});

})

Update :

Hey, I give same error again, and I notice new solution. If your code like this:

app.get('/', (req, res) => {
    res.send('TEST')
})


app.get('/:id', (req, res) => {

   var reqId = req.params.id
   var query = reqId.toString()

})

Could you try change your get request route. Maybe such as app.get('/findid/:id)

It worked for me. I was searching again and i found this way.

Thanks, Regards.

Calorimeter answered 5/2, 2022 at 17:45 Comment(0)
I
0

Sometimes clearing your local storage could be the solution to this error. From 'inspect' just go to Application>local storage and select the URL. Then press the 'clear all' sign. It worked for me.

Interlinear answered 11/5, 2022 at 8:14 Comment(0)
K
0

In my case when I have used this const product = await Product.findOne({'_id':id}), it solved the error

const {_id, name, description}=product||{}




export const read = async (req, res) => {
  try {
    //const product = await Product.findOne({ slug: req.params.slug })
    const product = await Product.findOne({'_id':id})
    
      .select("-photo")
      .populate("category");

    res.json(product);
  } catch (err) {
    console.log(err);
  }
};

enter image description here

Kabyle answered 14/8, 2023 at 16:15 Comment(0)
A
0

USE trim() I also have this error: In my case, it is due to the white space after the object ID that I passed. By using the trim() function

Astrakhan answered 3/9, 2023 at 15:45 Comment(1)
Your answer could be improved by providing an example of the solution and how it helps the OP.Coprophilia
F
0

In my case when i was facing issue, The User table has relations with Types table with reference of ID, I was passing string, instead of a ObjectId which is 24 hex character, so you mush check it once.

Felt answered 1/12, 2023 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.