我正在尝试设置一个 MeiliSearch 索引,以便在我的后端服务器(NodeJS/ExpressJS)启动时填充。下面是我的代码:
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)
});
除了我添加任何一个引用“res”的行之外,一切都很好。那时我得到错误:
res.status(500).json({
^
TypeError: Cannot read properties of undefined (reading 'status')