简体   繁体   English

猫鼬-找不到匹配的文档(ForEach)

[英]Mongoose - No matching document found (ForEach)

I'm new to NodeJS and Mongoose and this might be a duplicate question, so don't reply back negatively please. 我是NodeJS和Mongoose的新手,这可能是一个重复的问题,所以请不要负面答复。 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. 值会更新,但会立即更新几个字段,从而导致此错误。

Here's my code: (and I have added my github link to the project): 这是我的代码:(并且我已将github链接添加到项目中):

User.js (model) User.js(模型)

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

router.js 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");
                    }
                });
            }
        });
    });
});

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: 我添加了一个console.log来查看循环的运行,下图显示了第一次迭代的效果,但对于下一次迭代却表现得很奇怪: 在此处输入图片说明

I was thinking of breaking the loop after the id's match but the foreach loop doesnt have a break function, I changed it to a normal for loop but it still gives me the same error. 我本来想在id匹配后中断循环,但是foreach循环没有中断功能,我将其更改为普通的for循环,但它仍然给我同样的错误。 So I dont think breaking the loop is an answer.. 所以我不认为打破循环是一个答案。

Edit: Added image of my website to show what happens after updating (duplicates rows) 编辑:添加了我网站的图像以显示更新后发生的情况(重复行) 在此处输入图片说明

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

if break out of an iteration is your issue, then why not use a simple for loop with break statement. 如果爆发迭代是您的问题,那么为什么不使用带有break语句的简单for循环。

let eduArray = doc.local.education;
for(i=0;i<eduArray.length;i++) {
   if(eduArray[i]["id"] == row_id) {
      // do your logic
      break;
   }
}

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;

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

try to log the req.body to see what you actually assigning to the education. 尝试登录请求正文以查看您实际分配给该教育的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongoose - 版本错误:找不到与 id 匹配的文档 - Mongoose - Version Error: No matching document found for id Mongoose VersionError:保存文档时找不到匹配的ID文档 - Mongoose VersionError: No matching document found for id when document is being saved 猫鼬错误:`{“ message”:“找不到匹配的文档。”,“ name”:“ VersionError”}` - Mongoose Error: `{“message”:“No matching document found.”,“name”:“VersionError”}` 猫鼬在找不到指定字段的情况下创建文档,如果找到则更新文档中的数组 - Mongoose create a document if not found with specified fields, if found update an array in the document 检查 Mongoose (JavaScript) 中是否有符合特定条件的文档 - Checking if there is any Document matching a certain condition in Mongoose (JavaScript) 如果存在与 MongoDB 和 Mongoose 匹配的另一个文档,则添加字段 - Add field if another document matching exist with MongoDB & Mongoose 在Mongoose中过滤子文档数组,仅返回匹配的元素 - Filter sub document array in Mongoose and return only matching elements 将 object 更新或推送到找到的文档 mongoose 中的嵌套数组中 - Update or push object into nested array in found document mongoose ZCCADDEDB567ABAE643E15DCF0974E503Z forEach 不是 function - Mongoose forEach is not a function 在foreach中的mongoose find() - mongoose find() inside a foreach
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM