数据未在阵列上更新

时间:2019-05-26 19:00:35

标签: javascript node.js

我尝试过这个http端点来更新数组调用userArr上的数据,但是任何数据都不会被更新

app.put('/update/:Id',function (req, res) {

try{
const index = userArr.findIndex(instence => instence.id == req.params.Id);
userArr[index].firstName = req.body.fName;
userArr[index].lastName = req.body.lName;
userArr[index].birthDay = req.body.bDay;
res.status(200).send(userArr[index]);
}catch(e){
res.status(500).send({message:e});
}

预期输出为状态200,实际输出为500

1 个答案:

答案 0 :(得分:0)

那是因为您没有将ID值发送到API调用。您正在发送字符串“ ID” 应该是

var Id = "your id value";
app.put('/update/:'+ Id,function (req, res) {

try{
const index = userArr.findIndex(instence => instence.id == req.params.Id);
userArr[index].firstName = req.body.fName;
userArr[index].lastName = req.body.lName;
userArr[index].birthDay = req.body.bDay;
res.status(200).send(userArr[index]);
}catch(e){
res.status(500).send({message:e});
}