“VersionError: No matching document found” error on Node.js/Mongoose

后端 未结 5 1435
攒了一身酷
攒了一身酷 2020-12-13 17:34

I\'m relatively new to Node.js and Mongo/Mongoose, and I\'m having a very difficult time troubleshooting a specific Mongoose error:

VersionError: No

5条回答
  •  一整个雨季
    2020-12-13 18:14

    I had the same error when i tried to update a user's reference IDs to an email. The fix was really simple with async / await! Here the code snippet, hope it helps.

    email
        .save()
        .then(() =>
          User.findById(email.from).then(async sender => { // declare function as async
            sender.emails.sent.push(email._id);
            await sender.save(); // wait for save() to complete before proceeding
          }).catch((err) => console.log(err))
        )
        .then(() =>
          User.findById(email.to).then(async receiver => { // same as above
            receiver.emails.received.push(email._id);
            await receiver.save(); // same as above
          }).catch((err) => console.log(err))
        )
        .then(() => res.status(200).json({ message: successMessage }))
        .catch(err => console.log(err));
    

提交回复
热议问题