【问题标题】:Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arraysss找不到类型为“对象”的不同支持对象“[对象对象]”。 NgFor 只支持绑定到 Iterables 比如 Arraysss
【发布时间】:2022-11-19 01:50:40
【问题描述】:
import { HttpClient } from '@angular/common/http';
import { Component, OnInit} from '@angular/core';
import { AnyArray } from 'mongoose';
import { DataService } from '../../services/data.service';





@Component({
  selector: 'app-allpatients',
  templateUrl: './allpatients.component.html',
  styleUrls: ['./allpatients.component.css']
})
export class AllpatientsComponent implements OnInit {
  
  dataArray:any
  constructor(private ds:DataService){
    this.ds.getAllPatients().subscribe(data=>this.dataArray=data);
   
  }

  
 
ngOnInit(): void{

}
}

///////////////////////////////////////////data.service.ts/////////////////////////

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  constructor(private http:HttpClient) {  }

  getAllPatients(){
    return this.http.get('http://localhost:3000/patients')
  }



 
}

////////////////////////////my html////////////////////////////

<tr *ngFor="let item of dataArray ">
                            <td>{{item.firstname}}</td>
                            <td>{{item.lastname}}</td>
                            <td>{{item.age}}</td>
                            <td>{{item.phone</td>
                            <td>{{item.email}}</td>

我应该在我的代码中更改什么我不知道为什么它总是给我同样的错误

【问题讨论】:

    标签: angular web angular-material


    【解决方案1】:

    *ngFor 仅适用于数组等可迭代项,因此 dataArray包含一个对象。拨打http://localhost:3000/patients查看收到的内容

    另一方面,用空数组初始化 dataArray。

     dataArray:any = [];
    

    【讨论】:

    • 你好安东尼奥,所以我的localhost:3000/patients 包含名字、姓氏、年龄、电话、电子邮件的所有信息
    【解决方案2】:

    {“患者”:[{“_id”:“6374db3de99156199a548845”,“名字”:“yassine”,“姓氏”:“azaiez”,“电子邮件”:“yassine@yahoo.fr”,“年龄”:23,“ phone":92810192,"__v":0},{"_id":"6375045dd2eeac54c6ecb89b","firstname":"malik","lastname":"karim","email":"malik@yahoo.com"," age":12,"phone":999999,"__v":0},{"_id":"637607be05816c5a7829130d","firstname":"alex","lastname":"karima","email":"malik@ yahoo.com","age":12,"phone":999999,"__v":0},{"_id":"637607d105816c5a7829130f","firstname":"alex","lastname":"karima"," email":"malik@yahoo.com","age":12,"phone":999999,"__v":0},{"_id":"63768732bc19c7bb581a24af","firstname":"malik","lastname" :"azaiez","email":"zk@hotmail.com","age":18,"phone":97829101,"__v":0}],"user":null}

    这是我想在我的表上显示的内容,除了元素 id

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2018-10-12
      • 2020-08-29
      • 2023-01-26
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多