gpt4 book ai didi

javascript - TypeError : callback. apply is not a function (Node.js & Mongodb)

转载 作者:IT老高 更新时间:2023-10-28 13:32:12 29 4
gpt4 key购买 nike

当我添加“{ 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!'});
},
);
});

最佳答案

在 mongodb 中更新文档有两种方式:

  1. 找到文档,将其带到服务器,更改它,然后将其保存回 mongodb。

  2. 只要指示mongodb查找文件,修改即可;然后最后在mongodb完成后,将结果/错误作为回调返回。

在您的代码中,您正在混合使用这两种方法。


  1. 使用 user.save(),首先你使用 user.findOne 搜索数据库,然后将其拉到 server(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!' });
    });
  2. 第二个是使用 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!' });
});

关于javascript - TypeError : callback. apply is not a function (Node.js & Mongodb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299429/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com