类型错误: 无法解构'(intermediate value)'的属性'err',因为它未定义



我有一个带有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 });
}
}
);

这些是相应的控制器:(接受评估/拒绝评估)

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 };
}
};

当这个路由运行并执行操作时,我在控制台中得到这个错误

未处理的PromiseRejectionWarning:TypeError:无法销毁"(中间值)"的属性"err",因为它未定义。

我不知道问题出在哪里,析构函数或传递值有问题吗?

提前谢谢。

这是因为,你不是"返回";控制器的try块中的任何内容。return语句实际上是一个回调函数。

CCD_ 3用于代替回叫&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&抓箱子!

我有这个函数

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",因为它未定义。

相关内容

  • 没有找到相关文章

最新更新