Reading Object返回undefined Angular 4

时间:2018-01-22 11:03:31

标签: javascript angular

 Arr1 [
    {id: 300,
    uploads: [
        {idOne: value},
        {idTwo: value}
       ]
    },
    {blah: value, 
     uploads: [
        {idOne: value},
        {idTwo: value}
       ]
    }
]

正在读取此对象

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x)))

结果

对象如上所示。

并且

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x.id)))

显示ID:300

但是当我走了

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x.uploads)))

它记录未定义。我无法合理地发现原因。

这是在Angular组件的onInit()中。

1 个答案:

答案 0 :(得分:0)

试试这个,用forEach更新上面的内容: -

this.activatedRoute.parent.params.switchMap(params => {
 this.someService.getSubmissions(+params['id']))
 .subscribe(submissions => {
   submissions.forEach(x=>{
      if(x.id){
        console.log("id",x.id);
      }
      if(x.uploads){
      console.log("uploads",x.uploads);
      }

    })
 });
});