I can't do a search with the _id query on Mongodb

42 views Asked by At

I have a 'items' collection and I can't do a search with the _id query on it.

    await client.connect();
    const categoriesCollection = client.db('groseriarShop').collection('categories');
    const itemsCollection = client.db('groseriarShop').collection('items');

    console.log('Database connected')


    // items API
    app.get('/items', async (req, res) => {
        const query = {};
        const cursor = itemsCollection.find(query);
        const items = await cursor.toArray();
        res.send(items)
    });

    app.get('/items/:id', async (req, res) => {
        const id = req.params.id;
        const query = { _id: ObjectId(id) };
        ***const item = await itemsCollection.findOne(query);***
        console.log(item)
        res.send(item);
    })

My item is null. but why?

0

There are 0 answers