当我添加" {upsert:true}"时,我收到此错误:
TypeError:callback.apply不是函数
// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')
// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
.post(function(req, res) {
// use our user model to find the user we want
User.findOne({ userName: req.params.userName}, function(err, user) {
if (err)
res.send(err);
console.log('user.competitorAnalysis.firstObservation: %@', user.competitorAnalysis.firstObservation);
// Got the user name
var userName = user.userName;
// update the text data
console.log('Baobao is here!');
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation,
"competitorAnalysis.secondObservation" : req.body.secondObservation,
"competitorAnalysis.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.brandName" : req.body.brandName,
"competitorAnalysis.productCategory" : req.body.productCategory
} },
{ upsert: true }
);
// save the user
user.save(function(err) {
if (err)
return res.send(err);
return res.json({ message: 'User updated!' });
});
});
})
没有这一行,就没有错误。我是nodejs的新手,不太确定问题出在哪里。
更新
现在没有错误消息,但数据库的这一部分未使用新数据进行更新。嵌入的文档仍然是空的。
// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')
// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
.post(function(req, res) {
console.log('1');
// Just give instruction to mongodb to find document, change it;
// then finally after mongodb is done, return the result/error as callback.
User.findOneAndUpdate(
{ userName : req.params.userName},
{
$set:
{ "competitorAnalysis.firstObservation" : req.body.firstObservation,
"competitorAnalysis.secondObservation" : req.body.secondObservation,
"competitorAnalysis.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.brandName" : req.body.brandName,
"competitorAnalysis.productCategory" : req.body.productCategory
}
},
{ upsert: true },
function(err, user) {
// after mongodb is done updating, you are receiving the updated file as callback
console.log('2');
// now you can send the error or updated file to client
if (err)
return res.send(err);
return res.json({ message: 'User updated!' });
});
})
答案 0 :(得分:11)
有两种方法可以更新mongodb中的文档:
找到该文档,将其带到服务器,进行更改,然后将其保存回mongodb。
只是给mongodb指令找到文件,改变它;然后在完成mongodb之后,将结果/错误作为回调返回。
在您的代码中,您将混合使用这两种方法。
使用user.save(),首先使用user.findOne搜索数据库,并将其拉到服务器(nodejs),现在它存在于您的计算机内存中。 然后你可以手动更改数据,最后用user.save()
将其保存到mongodbUser.findOne({ userName: req.params.userName}, function(err, user) {
if (err)
res.send(err);
//this user now lives in your memory, you can manually edit it
user.username = "somename";
user.competitorAnalysis.firstObservation = "somethingelse";
// after you finish editing, you can save it to database or send it to client
user.save(function(err) {
if (err)
return res.send(err);
return res.json({ message: 'User updated!' });
});
第二个是使用User.findOneAndUpdate()..这是首选,而不是user.findOne()然后user.update();因为那些基本上两次搜索数据库。首先找到一个(),再次搜索更新()
无论如何,第二种方法是告诉mongodb更新数据而不首先引入服务器,接下来,只有在mongodb完成其操作后,您才会收到更新文件(或错误)作为回调
User.findOneAndUpdate({ userName: req.params.userName},
{
$set: { "competitorAnalysis.firstObservation" : req.body.firstObservation,
"competitorAnalysis.secondObservation" : req.body.secondObservation,
"competitorAnalysis.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.brandName" : req.body.brandName,
"competitorAnalysis.productCategory" : req.body.productCategory
} },
{ upsert: true },
function(err, user) {
//after mongodb is done updating, you are receiving the updated file as callback
// now you can send the error or updated file to client
if (err)
res.send(err);
return res.json({ message: 'User updated!' });
});
答案 1 :(得分:2)
您忘记将回调传递给update
方法
user.update(
{ $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation,
"competitorAnalysis.secondObservation" : req.body.secondObservation,
"competitorAnalysis.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.brandName" : req.body.brandName,
"competitorAnalysis.productCategory" : req.body.productCategory
} },
{ upsert: true },
function(err, result){}
);
update
方法需要3个参数。