【发布时间】:2021-03-22 15:40:57
【问题描述】:
我建立了一个连接到本地 MongoDB 的 Express 后端
当我创建帖子以查找特定术语时,它会返回这样的错误
MongoError: text index required for $text query
我该如何解决这个错误?
这是我的快递代码
app.post("/api/search",(req,res)=> {
const term = req.body.searchTerm;
console.log(term); // Here I can check the request coming well
MongoClient.connect(url,(err,db)=> {
if(err) throw err;
const dbo = db.db("Exercise");
dbo.collection("exercise")
.find({$text:{$search:term}})
.toArray((error, result) => {
if (error) {
console.log(error);
return;
} else {
console.log("Success");
res.send(result);
}
});
});
});
Thank you in advance!
【问题讨论】: