我正在创建一个休息API,我有帖子/电影的端点:请求正文应仅包含电影标题,并且应根据传递的标题验证其存在,其他电影详细信息应从thememoviedb获取,并保存到应用程序数据库。
app.post('/movies', (req, res) => {
request('https://api.themoviedb.org/3/discover/movie?callback=JSONP_CALLBACK&sort_by=popularity.desc&api_key=2931998c3a80d7806199320f76d65298', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred and handle it
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
});
db.collection('movies').findOneAndUpdate(req.body.title,{
title: 'Avengers',
},(err, result) => {
if (err) {
res.send({
'error': 'An error has occured'
});
} else {
res.send(result.ops[0]);
}
});
});
当我运行应用程序时,出现此错误,我在这里做错了什么?我是 Nodejs 新手,所有这些东西刚刚学习
最佳答案
在过滤器对象中使用$eq
运算符$eq
{ <field>: { $eq: <value> } }
所以最终的代码片段变成这样:
app.post('/movies', (req, res) => {
/* code ... */
let { title } = req.body
db.collection('movies').findOneAndUpdate({ title: { $eq: title } }, { title: 'Avengers' }, (err, result) => {
if (err) {
res.send({ 'error': 'An error has occured' });
} else {
res.send(result.ops[0]);
}
});
});
关于javascript - 蒙戈错误: filter parameter must be an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50858647/