我正在尝试以异步操作访问现有数组,但是由于某种原因,它变得不确定。
这是我的简化代码:
router.post(
"/:slug/create", auth,
async (req, res) => {
try {
const user = await User.findById(req.user.id);
const board = await Board.find({ slug: req.params.slug });
const newComment = {
title: req.body.title,
content: req.body.content,
name: user.name,
user: req.user.id,
};
board.comments.unshift(newProposal);
await board.save();
res.json(newComment);
} catch (err) {
console.error(err.message);
res.status(500).send("Server Error");
}
}
);
我正在尝试访问board.comments
。但是,即使存在board
且可以肯定地看到其中的comments[]
,为什么我仍然以Cannot read property 'unshift' of undefined
结尾?
非常感谢
编辑:console.log(面板)
[ { _id: '5d2335ada2dec216a6361063',
title: 'Test',
user: '5d205d1817e53a03ab11b2e8',
slug: 'test',
name: 'John Doe',
docs: [],
comments: [],
__v: 0 } ]