'BSONTypeError of Node Mongodb (server error)

When I try to get a document in my server from mongodb then "Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer" this error is showing. How can I fix this error? Thanks advaced.

Here is my code

async function run() {
    try {
        await client.connect();
        const fruitsCollection = client.db("fruits").collection("fruitsCollection");


        // multiple get api
        app.get('/fruits', async (req, res) => {
            const query = {};
            const cursor = fruitsCollection.find(query);
            const fruits = await cursor.toArray();
            res.send(fruits)
        });


        //  ******* my problem is here ****
        // single get api
        app.get('/fruits/:id', async (req, res) => {
            const id = req.params.id;
            const query = { _id: ObjectId(id) };
            const fruit = await cursor.toArray(query);
            res.send(fruit);
        });

    }

    finally {

    }
}

run().catch(console.dir);



app.get('/', (req, res) => {
    res.send('Hello World!')
});

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
});



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source