简体   繁体   中英

got some error massage in console when I try to update my field value in mongodb through express

My node app will crash when i send req for update the field value in my mongo db. The data will updated successfully, But The message will no show which is i provided in ( (err) => { if (err) { res.status(500).json({ error: "There was an error in server side,", }); } else { res.status(200).json({ message: "value updated successfully,", }); } } )

Instead of showing above message. the mongo sent me (const err = new MongooseError('Query was already executed: ' + str);). this message and more: (const err = new MongooseError('Query was already executed: ' + str);). this message and more: MongooseError: Query was already executed: Todo.updateOne({ _id: new ObjectId("6243ed2b5e0bdc9ab780b4d9...

But I use each and every time different id and update with differen message. when i check in db is that the old value updated or not, but nicely the old value updated. but no message or any thing i can't see in postman resposnse.Also in my console the mongodb throw me above error.

What ever happened I want to see my predefined err messages or successfully messages.

Finally, I got and understood the answer. Actually, I am a beginner programmer and developer that's why I made this problem and it took a lot of time to solve. I solved the problem within 3-5 minutes by removing async/await but I took a lot of time to dig out why it's working after removing async/await. Here is a basic concept of asynchronous programming if we use async/await we don't have to use callback again, Or if we use callback we don't need async/await Because it's just redundant. So, if we want to get data like we are getting from

callback(err,data) =>{
res.send(data);
}

then we just assigned our full thing in a variable like:

try{
const data=await (our code);
res.send(data);
}catch(err){
res.send(err.message);
}

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