TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined
TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined
我有一个带有 Node.js 后端的 React 应用程序。
我在后端有这条路线:
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 });
}
}
);
这些是相应的控制器:(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 };
}
};
当此路由运行并执行操作时,我在控制台中收到此错误
UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined.
我无法弄清楚问题出在哪里,解构或传递值有问题吗?
提前致谢。
这是因为,您没有“返回”控制器的 try
块中的任何内容。 return
语句实际上是回调函数。
await
用于替换回调 & promise.then
所以你必须在 try
块中 return
一些东西。您可以像这样实现:
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 };
}
}
你的控制器返回 try & catch 中的东西!
我有这个功能
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 };
}
};
并调用它
const { success } = await deleteMsg(userId, messagesWith, messageId);
尽管收到此错误
无法解构“(中间值)”的 属性 'success',因为它未定义。
我有一个带有 Node.js 后端的 React 应用程序。
我在后端有这条路线:
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 });
}
}
);
这些是相应的控制器:(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 };
}
};
当此路由运行并执行操作时,我在控制台中收到此错误
UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined.
我无法弄清楚问题出在哪里,解构或传递值有问题吗?
提前致谢。
这是因为,您没有“返回”控制器的 try
块中的任何内容。 return
语句实际上是回调函数。
await
用于替换回调 & promise.then
所以你必须在 try
块中 return
一些东西。您可以像这样实现:
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 };
}
}
你的控制器返回 try & catch 中的东西!
我有这个功能
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 };
}
};
并调用它
const { success } = await deleteMsg(userId, messagesWith, messageId);
尽管收到此错误
无法解构“(中间值)”的 属性 'success',因为它未定义。