【发布时间】:2018-11-24 07:11:48
【问题描述】:
我正在创建一个休息 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 新手,所有这些东西都在学习
【问题讨论】:
-
req.body.title不是“对象”。实际上,它看起来像是一个错字,因为接下来{ title: 'Avengers' }就是您的意思。或者可能是{ title: req.body.title }。简而言之,您似乎忘记删除测试并将变量添加到错误的位置。
标签: javascript mongodb express mongoose