I am creating a rest api I have end point for Post/Movies: Request body should contain only movie title, and its presence should be validated Based on passed title, other movie details should be fetched from thememoviedb,and saved to application database.
app.post('/movies', (req, res) => {
request(';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]);
}
});
});
when I run the app I get this error, what am I doing wrong here,? am new to nodejs and all this stuff just learning
I am creating a rest api I have end point for Post/Movies: Request body should contain only movie title, and its presence should be validated Based on passed title, other movie details should be fetched from thememoviedb,and saved to application database.
app.post('/movies', (req, res) => {
request('https://api.themoviedb/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]);
}
});
});
when I run the app I get this error, what am I doing wrong here,? am new to nodejs and all this stuff just learning
Share Improve this question asked Jun 14, 2018 at 13:25 Hot ZellahHot Zellah 1,0813 gold badges13 silver badges23 bronze badges 1-
1
req.body.title
is not an "object". Actually it looks like a typo as the next thing{ title: 'Avengers' }
is what you mean. Or probably{ title: req.body.title }
. In short, it looks like you forgot to remove your test and added the variable in the wrong place. – Neil Lunn Commented Jun 14, 2018 at 13:26
2 Answers
Reset to default 4Use $eq
operator in the filter object $eq
{ <field>: { $eq: <value> } }
So the final snippet bees like this:
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]);
}
});
});
Try the following,
db.collection('movies').findOneAndUpdate({title:req.body.title},{
$set:{
'Avengers'
}})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743884269a4523739.html
评论列表(0条)