【发布时间】:2019-07-12 04:55:58
【问题描述】:
我正在尝试使用 MongoDB 和 nodejs 构建一个应用程序。 我有一个模型Ride,其中包含一个名为steps 的用户ID 数组。我想从由其自己的 RideID 指定的 Ride 中删除一个 userID 条目。
我尝试了以下代码,但它不起作用,总是引发错误
router.post('/quitRide',async (req,res)=>{
let userID = req.body.userID; //userID to be deleted
let rideID = req.body.rideID; //rideID of the Ride we want to access
Ride.find({_id:rideID})
.exec()
.then(t => {
t[0].steps.splice(t[0].steps.indexOf(userID), 1);
res.status(200);
})
.catch(err => res.status(400).json({error : err}))
})
这是我得到的错误:
{错误:“erreurTypeError: 无法读取未定义的属性‘indexOf’”}
似乎无法访问步骤
【问题讨论】:
-
在有问题的行前添加
console.log( t[0] );(或使用调试器检查)。 -
是否有没有
steps的游乐设施(可能是新创建的游乐设施?)