[英]TypeError: callback.apply is not a function (Node.js & Mongodb)
當我添加“{ upsert: true }”這一行時,我收到了這個錯誤:
類型錯誤: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!'});
},
);
});
mongodb 有兩種方式更新文檔:
找到該文檔,將其帶到服務器,進行更改,然后將其保存回 mongodb。
只是給mongodb指令尋找文檔,改變它; 然后最后在 mongodb 完成后,將結果/錯誤作為回調返回。
在您的代碼中,您混合了這兩種方法。
使用 user.save(),首先你用 user.findOne 搜索數據庫,並將它拉到服務器(nodejs),現在它存在於你的計算機內存中。 然后您可以手動更改數據,最后使用 user.save() 將其保存到 mongodb
User.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(); 因為那些基本上搜索數據庫兩次。 先findOne(),再搜索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!' });
});
您忘記將回調傳遞給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 個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.