0

I'm trying to setup a MeiliSearch index to be populated when my backend server (NodeJS/ExpressJS) starts. Below is my code:

const withDB = async (operations, res) => {
try {
    const client = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
    const db = client.db('database-name');

    await operations(db);

    client.close();
} catch (error) {
    res.status(500).json({ message: 'Error connecting to db', error });
}
}

app.listen(8000, async(res) => {
withDB(async (db) => {
    const searchReports = await db.collection('reports').find().limit(500).toArray()
    res.status(200).json(searchReports);
    const searchIndex = new MeiliSearch({ host: 'http://127.0.0.1:7700' })
    searchIndex.index('Reports').addDocuments(searchReports) 
        .then((res) => console.log(res));

}, res)

});

Everything works fine apart from when I add either of the line referencing 'res'. At that point I get the error:

            res.status(500).json({
                ^

TypeError: Cannot read properties of undefined (reading 'status')
  • app.listen callback function does not have any parameter, that the reason res is undefined & you're getting the error for using res.status – Rahul Pal Jan 20 '22 at 15:25
  • I'm not sure I follow sorry? I'm reading the express docs and can't see where I'm going wrong. – Nathan Field Jan 20 '22 at 22:03

0 Answers0