【问题标题】:How to print data from node Api to Angular Table.? While printing data it's showing undefined element如何将数据从节点 Api 打印到 Angular 表。?打印数据时显示未定义的元素
【发布时间】:2021-06-26 03:57:00
【问题描述】:

我正在尝试在角度表中打印数据,数据来自 JSON 格式的节点。但是,即使在控制台数据中打印 t 也显示未定义。 我也附上了 JSON 响应图片。

我正在使用 API 从 MongoDB 获取数据并使用 API 向 Angular 发送数据。数据即将到来,但问题是它没有在表中打印。

 <table class="table table-bordered table-striped ">
            <thead>
              <tr>
                <th>Username</th>
                <th>Date registered</th>
                <th>Role</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>
              <tr *ngFor="let x of data ">
                <td>{{x.serial}}</td>
                <td>2012/01/01</td>
                <td>Member</td>
                <td>
                  <span class="badge ba+dge-success">Active</span>
                </td>
              </tr>
            
            </tbody>
          </table>

来自 API 的 JSON 响应

{
code: 0,
message: "success",
data: {
labels: [
{
_id: "605b262c36c70c354015a945",
serial: "12345",
sourceApi: "ASDASD",
resultApi: "ASDASD",
labelApi: "ASDASD",
__v: 0
},
{
_id: "605b26bd36c70c354015a946",
serial: "3333",
sourceApi: "sdfdg",
resultApi: "sdsf",
labelApi: "gggg",
__v: 0
},
{
_id: "605b26ea2e5ea63080dd4796",
serial: "3333",
sourceApi: "sdfdg",
resultApi: "sdsf",[enter image description here][1]
labelApi: "gggg",
__v: 0
}
]
}
}

TS 文件代码

export class TablesComponent implements OnInit{
  public data?:any;
  constructor(private api:ApiServiceService) { 

    this.api.getDataApi().subscribe(data => {
      this.data = data;
    })
   

  }

 

  dtOptions: any = {}; 
  ngOnInit(){
  this.dtOptions = {
    "paging"  : true,
    "ordering": true,
    "info"    : true
  };

  }

【问题讨论】:

  • 尝试使用this.data = data[0]。如果您可以添加 JSON 响应的图像,那就更好了。
  • 在描述中添加 JSON 响应 sir { code: 0, message: "success", data: { labels: [ { _id: "605b262c36c70c354015a945", serial: "12345", sourceApi: "ASDASD ", resultApi: "ASDASD", labelApi: "ASDASD", __v: 0 }, { _id: "605b26bd36c70c354015a946", 序列号: "3333", sourceApi: "sdfdg", resultApi: "sdsf", labelApi: "gggg", __v: 0 }, { _id: "605b26ea2e5ea63080dd4796", serial: "3333", sourceApi: "sdfdg", resultApi: "sdsf", labelApi: "gggg", __v: 0 } ] } } 我想得到标签跨度>

标签: node.js angular node-modules


【解决方案1】:

您的 JSON 包含一些其他不需要填充数据的数据。

在您的 JSON 标签中包含一个对象数组,因此我们必须将其分配给“数据”变量

export class TablesComponent implements OnInit{
public data?:any;
constructor(private api:ApiServiceService) { 

this.api.getDataApi().subscribe(data => {
  this.data = data.data.labels;
})

}

【讨论】:

  • 当我添加第二个 .data 时,它显示“对象类型上不存在属性”
  • this.data = data.data['labels'];
  • core.js:6162 ERROR 错误:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
  • {{x?.serial}} 2012/01/01 会员 活跃
  • data.data['labels'];
猜你喜欢
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2017-12-09
  • 2018-01-10
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多