'TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined
I have a React application with a Node.js backend.
I have this route on the backend:
router.put(
'/testimonial/:testimonialId/:action',
[authenticate, validateUser],
async (req, res) => {
if (req.params.action === 'ACCEPT') {
const { err, status, data } =
await notificationController.acceptTestimonial(
req.params.testimonialId,
req.user._id
);
res.status(status).send({ err, data });
} else if (req.params.action === 'REJECT') {
const { err, status, data } =
await notificationController.rejectTestimonial(
req.params.testimonialId,
req.user
);
res.status(status).send({ err, data });
}
}
);
These are the corresponding Controllers: (acceptTestimonial/rejectTestimonial)
const acceptTestimonial = async (testimonialId, userId) => {
try {
await Users.findOne({ _id: userId }, async function (err, creator) {
if (err) return { err, status: 404, data: null };
else {
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id == testimonialId) {
testimonial.status = 'PUBLISHED';
}
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
}
});
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
};
const rejectTestimonial = async (testimonialId, userId) => {
try {
await Users.findOne({ _id: userId }, async function (err, creator) {
if (!creator)
return { err: 'Creator not found!', status: 404, data: null };
else {
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id === testimonialId) {
testimonial.status = 'REJECTED';
}
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
}
});
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
};
When this route runs and performs the operations, I get this error in the console
UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined.
I'm unable to figure out what the problem is, is there a problem with destructuring or passing values ?
Thanks in advance.
Solution 1:[1]
it is because, you aren't "returning" anything in try block of your controllers. The return statement is actually of callback function.
await is used to replace callback & promise.then so you have to return something in try block. You can achieve this like:
const rejectTestimonial = async (testimonialId, userId) => {
try {
// await Users.findOne({ _id: userId }, async function (err, creator) {
// if (!creator)
// return { err: 'Creator not found!', status: 404, data: null };
// else {
// const updatedTestimonials = creator.testimonials.map((testimonial) => {
// if (testimonial._id === testimonialId) {
// testimonial.status = 'REJECTED';
// }
// return testimonial;
// });
// creator.testimonials = updatedTestimonials;
// await creator.save();
// return { err: null, status: 200, data: creator };
// }
// });
// ? instead of this
// ? do this
const creator = await Users.findOne({ _id: userId }); // for "_id", you can also simply do: await User.findById(userId);
if (!creator)
return { err: 'Creator not found!', status: 404, data: null };
// creator.testimonials.forEach((testimonial) => {
// if (testimonial._id === testimonialId)
// testimonial.status = 'REJECTED';
// });
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id === testimonialId)
testimonial.status = 'REJECTED';
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
}
Here your controller is returning something in both try & catch cases!
Solution 2:[2]
I have this function
const deleteMsg = async (userId, messagesWith, messageId) => {
try {
const user = await Chat.findOne({ user: userId });
const chat = user.chats.find(
chat => chat.messagesWith.toString() === messagesWith,
);
if (!chat) {
return;
}
const messageToDelete = chat.messages.find(
message => message._id.toString() === messageId,
);
if (!messageToDelete) return;
if (messageToDelete.sender.toString() !== userId) {
return;
}
const indexOf = chat.messages
.map(m => m._id.toString())
.indexOf(messageToDelete._id.toString());
await chat.messages.splice(indexOf, 1);
await user.save();
return { success: true };
} catch (error) {
console.error(error);
return { success: false };
}
};
and calling it
const { success } = await deleteMsg(userId, messagesWith, messageId);
getting this error though
Cannot destructure property 'success' of '(intermediate value)' as it is undefined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | VEDANT KARLE |
