• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

같은코드인데 에러가납니다.

21.10.08 10:17 작성 조회수 97

0

{
"err": "The path 'comments' must exist in the document in order to apply array updates."
}
 
모델은 강의코드와 같은상태이고
 
route 의 put부분도 똑같은 상태입니다. 해당에러로 구글링을 해보았지만 잘 이해가안되어서 질문을 드립니다 !!
 
혹시 이런경우에는 어떤것을 의심해야하나요? Blog Model에 comments 가 정상적으로 있는데 저 에러가 나서요...!
 
userRouter.put('/:userId', async (req, res) => {
try {
const { userId } = req.params;
if (!mongoose.isValidObjectId(userId))
return res.status(400).send({ err: 'invalid userId' });
const { age, name } = req.body;
if (!age && !name)
return res.status(400).send({ err: 'age or name is required' });
if (age && typeof age !== 'number')
return res.status(400).send({ err: 'age must be a number' });
if (name && typeof name.first !== 'string' && typeof name.last !== 'string')
return res.status(400).send({ err: 'first and last name are strings' });
// let updateBody = {};
// if(age) updateBody.age = age;
// if(name) updateBody.name = name;

// const user = await User.findByIdAndUpdate(userId, updateBody, { new: true });
const user = await User.findById(userId);
if (age) user.age = age;
if (name) {
user.name = name;
await Promise.all([
Blog.updateMany({ 'user._id': userId }, { 'user.name': name }),
Blog.updateMany(
{},
{ 'comments.$[comment].userFullName': `${name.first} ${name.last}` },
{ arrayFilters: [{ 'comment.user': userId }] }
),
]);
}
await user.save();
return res.send({ user });
} catch (err) {
console.log(err);
return res.status(500).send({ err: err.message });
}
});
 

답변 1

답변을 작성해보세요.

0

데이터를 정규화식으로 저장했던 것들 삭제하고 하셨나요? 오류메시지 보시면 blog에 comments 배열이 없어서 오류가 난다고 나오네요