简体   繁体   中英

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. 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):

User.js (model)

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

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: 在此处输入图片说明

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. 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

if break out of an iteration is your issue, then why not use a simple for loop with break statement.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM