typescript - 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);
});
});
解决方案
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);
});
});
推荐阅读
- c# - 哪个生命周期最适合在 ASP.Net MVC 中使用带有 Autofac 的实体框架?
- python - Sportsreference Python为已安排比赛的日期返回空字典
- r - R:“循环”期间遇到的错误:x Input `name` 无法回收到大小 100
- arrays - 编写 VBA,将数组第 2 列中的每个元素设置为大小为 n 的新嵌套数组,其中 n 是第 1 列中的整数
- python - 有没有一种简单的方法可以在 Python 3 中进行多行输入?
- mysql - MySQL 如何启用加载本地数据服务器/客户端(使用 Powershell)
- sql - 3 行到 1 个 SQL
- uitableview - 如何使用 JSON 数据填充自定义 UITableViewCell?
- cassandra - Cassandra 何时确认写入?
- numbers - 增加产生最后一位数和效率顺序的素数