본문 바로가기
오늘의뻘짓

[따라하며 배우는 노드js] 7강 오류

by 강갱민 2023. 11. 22.

발견한오류

 

C:\Users\SSAFY\Desktop\nodejs\boilerplate\node_modules\mongoose\lib\model.js:517 throw new MongooseError('Model.prototype.save() no longer accepts a callback');

 

원인

오류 메시지 MongooseError: Model.prototype.save() no longer accepts a callback는 최신 버전의 Mongoose에서 save() 메소드가 더 이상 콜백 함수를 받지 않는다,

 

해결방안

 

기존 코드

app.post('/register', async (req, res) => {

 const user = new User(req.body)
 user.save((err,doc)=>{
  if(err) return res.json({success:false,err})
  return res.status(200).json({
success:true,})
 })

})

 

 

수정코드 

app.post('/register', async (req, res) => {
  const user = new User(req.body);

  try {
    const doc = await user.save();
    res.status(200).json({ success: true });
  } catch (err) {
    res.json({ success: false, err });
  }
});