findOneAndUpdate()不再接受回调函数。

huangapple go评论59阅读模式
英文:

findOneAndUpdate() no longer accepts a callback

问题

我是一名正在学习全栈开发的学生。

我正在尝试完成一个任务,创建一个允许用户检索电影列表等详细信息的应用程序。

有一个 app.put,用户可以更新他们的用户名:

// UPDATE - 用户更新用户名

app.put('/users/:Username', (req, res) => {
    Users.findOneAndUpdate (        
        { Username: req.params.Username},
        {
            $set: {
                Username: req.body.Username,
                Password: req.body.Password,
                Email: req.body.Email,
                Birthday: req.body.Birthday
            }
        },
        { new: true },
        (err, updatedUser) => {
            if (err) {
                console.error(err);
                res.status(500).send('错误:' + err);
            } else {
                res.json(updatedUser);
            }
        })
    ;
});

我正在使用 Postman 尝试上述代码。

它一直报错 findOneAndUpdate() 不再接受回调函数。

我一直在努力找出如何解决这个问题,但一直没有成功。

请问您能否提供修复代码的建议?我已经尝试过搜索但无法解决。

英文:

I am a student currently learning on full-stack.

I'm trying to complete an assignment where I am creating an app which allows users to retrieve details on list of movies etc.

There's this app.put where users can update their username:

// UPDATE - user update username

app.put('/users/:Username', (req, res) => {
    Users.findOneAndUpdate (        
        { Username: req.params.Username},
        {
            $set: {
                Username: req.body.Username,
                Password: req.body.Password,
                Email: req.body.Email,
                Birthday: req.body.Birthday
            }
        },
        { new: true },
        (err, updatedUser) => {
            if (err) {
                console.error(err);
                res.status(500).send('Error: ' + err);
            } else {
                res.json(updatedUser);
            }
        })
    ;
});

I am using Postman to try out above code.

It keep stating findOneAndUpdate() no longer accepts a callback.

I have been trying to figure out how to solve it but to no avail.

Could you advise on how to fix the code please?

Have tried googling but can't solve

答案1

得分: 0

回调函数在Mongoose v7.x中已被删除。阅读更多这里

解决方案

使用try catch代替!而且在mongoose中,你需要在Model.query({})之前使用async函数和await关键字。

更新后的代码:

app.put('/users/:Username', async function(req, res) {
    let updatedUser;
    try{
        updatedUser = await Article.findOneAndUpdate(
        { 
            Username: req.params.Username
        },
        {
            $set: {
                Username: req.body.Username,
                Password: req.body.Password,
                Email: req.body.Email,
                Birthday: req.body.Birthday
            }
        },
        { 
            new: true 
        });
    }
    catch(err){
        console.error(err);
        return res.status(500).send('Error: ' + err);
    }
    return res.json(updatedUser);
}

希望对你有所帮助!

英文:

Call back functions were dropped in Mongoose v7.x. Read more here.

Solution

Use try catch instead! And mongoose you need to use an async function and await keyword before your Model.query({}).

Updated code:

app.put('/users/:Username', async function(req, res) {
    let updatedUser;
    try{
        updatedUser = await Article.findOneAndUpdate(
        { 
            Username: req.params.Username
        },
        {
            $set: {
                Username: req.body.Username,
                Password: req.body.Password,
                Email: req.body.Email,
                Birthday: req.body.Birthday
            }
        },
        { 
            new: true 
        });
    }
    catch(err){
        console.error(err);
        return res.status(500).send('Error: ' + err);
    }
    return res.json(updatedUser);
}

Hope this helps!

huangapple
  • 本文由 发表于 2023年6月30日 00:03:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定