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
2
Answers
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:Consider the following Javascript example that reproduces the error:
The
req
variable comes from the client calling thispostDeleteProduct()
function: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:Per your comment:
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.
You have posted two errors
Lets talk about the first one first:-
Error:- process reaching
then
scope whether the above deleteOne happens or not.Answer:-
From the code that you posted I assume
User
is the mongoose/mongodb model on which you are applying the mongoose / mongodbdeleteOne
function.In that case irrespective of whether it matches the given condition or not it will reach the
.then()
function. BecozdeleteOne
returns:-So in your
then
scope you need to check for the value ofdeletedCount
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
Now coming to your next error.
Error:-
its becoz
req.session.userAuth._id
hereuserAuth
is not defined.you can console log the
req.session
thing and see whether theuserAuth
is set there or not. Its unrelated with the current code or you are calling wrong property name.