猫鼬-找不到匹配的文档(ForEach) [英] Mongoose - No matching document found (ForEach)

查看:67
本文介绍了猫鼬-找不到匹配的文档(ForEach)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NodeJS和Mongoose的新手,这可能是一个重复的问题,所以请不要负面地回复.我试图找到一种解决方案,但未能解决此错误.

I'm new to NodeJS and Mongoose and this might be a duplicate question, so don't reply back negatively please. I tried finding a solution but failed to fix this error.

基本上,当我尝试更新数据库值时出现此错误.我执行的第一个更新工作正常,但是从第二个开始,出现此错误.这些值会更新,但会立即更新几个字段,从而产生此错误.

Basically, I get this error when I try to update the database values. The first update I perform works perfectly but from the second one onward, I get this error. The values update but it updates several fields at once giving this error.

这是我的代码:(并且我已经将github链接添加到了项目中):

Here's my code: (and I have added my github link to the project):

User.js(模型)

local: {
    ...,
    education: [{
        id: String,
        degree: String,
        institute: String,
        dates: String,
        description: String
    }],
    ...
}

router.js

app.put('/put_education/:id', function(req, res) {
    var row_id = req.params.id; //get education id from table row

    //get the current logged in user
    User.findById(req.user._id, function (err, doc) {
        if (err) {
            console.log('no entry found');
        }

        //match the table row id with the id in users education model
        doc.local.education.forEach(function (education, index) {
            console.log(education.id + "   " + row_id);

            //if rows match, replace the database document with values from client
            if(education.id === row_id){
                doc.local.education[index] = req.body;
                doc.save(function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        res.send("Success");
                    }
                });
            }
        });
    });
});

我添加了一个console.log来查看循环的运行,下图显示了第一次迭代的效果,但对于下一次迭代却表现得很奇怪:

I added a console.log to see the loop operates, the image below shows its fine for the first iteration but acts weirdly for the next ones:

我本来想在id匹配后中断循环,但是foreach循环没有break函数,我将其更改为普通的for循环,但它仍然给我同样的错误.所以我不认为打破循环是一个答案.

添加了我网站的图像,以显示更新后发生的情况(重复行)

Added image of my website to show what happens after updating (duplicates rows)

Github: https://github.coventry.ac.uk/salmanfazal01 /304CEM-Back-End

推荐答案

因为您是第一次更新教育,没有问题,然后又出现了问题,我怀疑您错误地更新了教育,请看这段代码你写道:

becuase you are updating the education first time with no problem, then the problem appears afterwards, I suspect that you update the education wrongly, look into this line of code you wrote:

doc.local.education [index] = req.body;

doc.local.education[index] = req.body;

在这里我看到您正在将请求正文中的任何内容分配给教育,但是您是否检查了req.body中实际上是什么数据?

here I see you are assigning whatever inside the request body to education, but have you checked what data is actually inside the req.body?

尝试登录需求正文,以查看您实际分配给该教育的内容.

try to log the req.body to see what you actually assigning to the education.

这篇关于猫鼬-找不到匹配的文档(ForEach)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆