【问题标题】:Angular- I got 'undefined' as an output of an array object while I have data in my array objectAngular-当我的数组对象中有数据时,我得到了“未定义”作为数组对象的输出
【发布时间】:2021-01-09 23:39:39
【问题描述】:

我有一个 API,其响应在下面的屏幕截图中给出。我想获取我的数组对象的数据并显示在网页上。但是我得到了未定义的输出,而我的数组对象中有数据。请告诉我哪里做错了。

服务代码:

testfxn(){
  return(
    this.apiCallService.GET(`${this.myService.myendpoint.xyz}`)
    .pipe(
      map((testData)=>{
        let testresp=testData.response[4];
        return testresp;
      })
    )
  );
}

component.ts:

    testing(){
        this.reportService.testfxn().subscribe((r)=>{
          this.resp=r
          console.log(this.resp)
        })
      }

【问题讨论】:

    标签: arrays angular api object


    【解决方案1】:
    testing(){
        this.reportService.testfxn().subscribe((data) => {
          this.testingdate=data;
          console.log(this.testingdate);
        });
      }
    

    testfxn() 是一个异步函数。您的console.log 可能在函数返回数据之前被执行并被subscribe() 使用

    【讨论】:

    • 仍然得到相同的输出
    • 我无法在我的 service.ts 文件下使用 console.log(testData)。 IDE 不允许我这样做。
    • 等等,你的testData.userLogtime[0] 没有bifurData 对象。你的意思是data.date
    • 日期是我的数组对象的属性,数组对象的名称是userLogtime,如截图所示
    • 是的。您将从服务中的 userLogtime 数组返回第一个对象。在您的组件中,您尝试访问bifurData,这不是您要返回的对象的属性。
    猜你喜欢
    • 1970-01-01
    • 2020-08-10
    • 2021-07-15
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多