Home > Back-end >  NodeJS (express) - Javascript promise is being executed whether success or failure, How can I make a
NodeJS (express) - Javascript promise is being executed whether success or failure, How can I make a

Time:10-22

exports.postDeleteProduct = (req, res, next) => {
  const userId = req.body.userId;
  User.deleteOne({ _id: userId, userId: req.session.userAuth._id })
    .then(() => {
      console.log("DESTROYED PRODUCT"); //process reaching  here whether the above deleteOne happens or not
      res.redirect("/users");
    })
    .catch((err) => console.log(err));
};

Also, the code works great, but for some reason gave this error ONLY ONCE, but then continued working normally.. without me even changing anything.. which is scary. Anyway, here's the error that popped up out of nowhere then disappeared:

TypeError: Cannot read properties of undefined (reading '_id')

This error showed once, then I restarted nodemon, everything worked great... weird! But it's the reason I'm asking for help, because I think it's something to do with my faulty promise which always executes.

A bit more details of what happens when I click delete:

  • if the condition meets: "DESTROYED PRODUCT" gets logged
  • if the condition does NOT MEET: "DESTROYED PRODUCT" gets logged also

CodePudding user response:

There are a few things here. The first is that the source of the error should be double checked. Without the information from the stack trace, we can't say for sure that the error is coming from this particular snippet of code. It certainly could though, and the rest of the comment assumes that is the case.

The error message in general suggests that the code is failing to access the _id value of an object. The only place in this code snippet where that happens is when attempting to construct the query predicates, specifically:

req.session.userAuth._id

Consider the following Javascript example that reproduces the error:

> req
{ session: {} }
> req.userAuth
undefined
> req.userAuth._id
Uncaught TypeError: Cannot read properties of undefined (reading '_id')
>

The req variable comes from the client calling this postDeleteProduct() function:

exports.postDeleteProduct = (req, res, next) =>

So there is nothing in this snippet of code that is generating an invalid object. Rather there is some upstream code that calls this function and does not seem to have the structure for the req parameter that this code is expecting. So the answer to this question would seemingly be:

  1. This code snippet should check to make sure that the structure of the parameters provided to it are as expected and gracefully respond if not.
  2. Check the upstream code that is calling this function to better understand how this variable is being generated and why the object isn't as expected.

Per your comment:

This makes sense, instead of just using " : " to check if they are equal, I should try using an if else block so that if they aren't, I have more output options, sounds about right?

This doesn't sound correct. The error is being thrown before the query is even being sent to the database while the application is still attempting to formulate the predicates. Per above, this code should validate the arguments that are being passed to it prior to attempting to build the query to send to the database.

CodePudding user response:

From the code that you posted I assume User is the mongoose/mongodb model on which you are applying the mongoose / mongodb deleteOne function. In that case irrespective of whether it matches the given condition or not it will reach the .then() function. Becoz deleteOne returns:-

{
   "acknowledged" : true,
   "deletedCount" : 1.0
}
  1. acknowledged => acknowledges the requested action i.e deleteOne
  2. deletedCount => this will be either 1 or 0 based on the match condition found a deletable document. if found, deletedCount is 1 and if not found it will be 0.

So in your then scope you need to check for the value of deletedCount to actually understand whether the document is deleted or not.

For further reference you can go through the official documentation. https://mongoosejs.com/docs/api/model.html#model_Model-deleteOne

  • Related