Data is not fetched, empty array is returned

Viewed 36

So I'm facing the problem where I'm not getting proper data from a database. My database is successfully connected. As a result I'm getting the empty array of data.

Here is what I'm trying to fetch:

app.get('/user', async (req,res) => {
    let db = await connect()
    
    let cursor = await db.collection("user").find({})
    let finalData = await cursor.toArray();
    
    res.json(finalData);
})
1 Answers

Not exact but I used this example in the past using

Note: this is not my code, but an answer I had used when struggling in the same way

    const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://localhost:27017';

async function findOne() {

    const client = await MongoClient.connect(url, { useNewUrlParser: true })
        .catch(err => { console.log(err); });

    if (!client) {
        return;
    }

    try {

        const db = client.db("testdb");

        let collection = db.collection('cars');

        let query = { name: 'Volkswagen' }

        let res = await collection.findOne(query);

        console.log(res);

    } catch (err) {

        console.log(err);
    } finally {

        client.close();
    }
}

await findOne();

I think from this example you could ascertain an answer

Related