Mateo Díaz
Mateo Díaz

Reputation: 35

get method in Express-mongodb " Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"

I'm currently working in an api with mongodb-nodejs-express implementing a get method that finds all stores in my database using its id, the problem is I keep receiving the following error when I use the get method:

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

This is the link I'm using to prove

http://localhost:4005/trading/store/1124

This is the method I've implemmented

router.get('/store/:id', async (req, res) => {
    const { id } = req.params;
    const db = await connect();
    try{
    const result = await db.collection(collection).find({ store_id: ObjectID(id)});
    res.json(result)
    }
    catch (error) {
        res.status(500).json({ error: error.toString() });
    }
})

This is an example of how my db looks

{
        "_id": "5cef828a7443855d02fc320e",
        "timestamp": "26/03/2019 18:34",
        "store_id": "1124",
        "user_id": "123",
        "product_id": "949",
        "price": "528"
    },

I would really appreciate any hint or help and thanks in advance for taking the time to read my question.

Upvotes: 1

Views: 362

Answers (1)

dimitris tseggenes
dimitris tseggenes

Reputation: 3186

Your strore_id is just a string. So your query should be like this:

db.collection.find({ store_id: id});

Upvotes: 1

Related Questions