我有一個帶有 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: 無法解構 '(intermediate value)' 的屬性 'err',因為它是未定義的。
我無法弄清楚問題是什么,解構或傳遞值是否有問題?
提前致謝。
uj5u.com熱心網友回復:
這是因為,您沒有在try控制器塊中“回傳”任何東西。該return陳述句實際上是回呼函式。
await用于替換回呼 &promise.then所以你必須return在try塊中做一些事情。您可以像這樣實作:
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 兩種情況下都回傳了一些東西!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322879.html
