首页 > 解决方案 > CastError: 值“{ userId: '5c48a95df9bd9a33c0ff9405' 转换为 ObjectId 失败”

问题描述

'Cast to ObjectId 的值“{ userId: \'5c48a95df9bd9a33c0ff9405\',\n username: \'ahsan\',\n iat: 1549024353,\n exp: 1549110753 }” 在模型“Rental”的路径“user”处失败'

router.get("/manage", UserControl.loginMiddleware, (req, res) => {
const user = res.locals.user;
Rental.where({ user })
.populate("bookings")
.exec((err, foundRentals) => {
  if (err) {
    return res.status(422).send({ errors: mongoErrors(err.errors) });
  }
  return res.json(foundRentals);
  });
});

标签: typescript

解决方案


router.get("/manage", UserControl.loginMiddleware, (req, res) => {
const user = res.locals.user;
Rental.where("user",user.userId)
.populate("bookings")
.exec((err, foundRentals) => {
  if (err) {
  return res.status(422).send({ errors: mongoErrors(err.errors) });
     }
 return res.json(foundRentals);
   });
});

推荐阅读