Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度6-方法始终返回-1_Javascript_Angular_Typescript_Angular6 - Fatal编程技术网

Javascript 角度6-方法始终返回-1

Javascript 角度6-方法始终返回-1,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我有一个服务,我正在尝试更新一个记录,这只是将更改添加到组件的数据中 listData保存所有数据 变量都包含数据 示例:1,1,我的标题,帖子文本 结果正在成功返回到服务中。但在我 update(id, userId, title, body) { this.apiService.updateById(id, userId, title, body).subscribe( result => { console.log(result);

我有一个服务,我正在尝试更新一个记录,这只是将更改添加到组件的数据中

listData保存所有数据

变量都包含数据

示例:1,1,我的标题,帖子文本

结果正在成功返回到服务中。但在我

update(id, userId, title, body) {
    this.apiService.updateById(id, userId, title, body).subscribe(
      result => {
        console.log(result);

        // PROBLEM STARTS HERE
        const currentItemIndex = this.listData.findIndex((item: {id: number}) => item.id === id);

        console.log(`CurrentIndex is: ${currentItemIndex}`); // Returns -1

        // THIS IS NEVER REACHED
        if (currentItemIndex > -1) {
            this.listData.splice(currentItemIndex, 0, {userId: userId, title: title, body: body});
        }

      },
      error => {
      console.log('There was an error: ', error); // No errors returned
    }
  );

}

我怎样才能解决这个问题?有什么问题吗?

请使用findIndex作为:

this.listData.findIndex((item) => item.id === id);
我不认为显示
项的类型是
findIndex

let listData=[
{
“用户ID”:1,
“id”:1,
“标题”:“除可选择的补救措施外,必须重新制定计划”,
“正文”:“要求和支持\n要求专业人员和\n专业人员在设计和施工前对环境进行保护\n要求建筑设计师在设计和施工前对环境进行保护”
},
{
“用户ID”:1,
“id”:2,
“头衔”:“我最爱你”,
“身体”:“暂时性生命是指身体的健康状况,是指身体的健康状况,是指身体的健康状况,是指身体的健康状况,是指身体的健康状况,是指身体的健康状况。”
}]
//假设我的身份是2
设id=2;
让foundIndex=listData.findIndex((eachData)=>{
//===当它是和整数时,它是否在您的情况下??
//如果没有,则尝试使用==
返回eachData.id==id;
});

console.log(foundIndex)
什么是内容列表数据该变量保存此数据:仍然给我-1@MarkHill2018:好的,您能在查找索引之前
console.log(id)
吗?好的,id是正确的。如果我试图编辑ID为2的记录。。。id将返回2@MarkHill2018:是字符串还是整数?尝试使用
=
而不是
=