Home > Net >  Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor

Time:11-19

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{

}
}

/////////////////////// and this is my 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')
  }



 
}

So what should I change so it can work?

please I'm stuck and I don't know what I should change on my code

CodePudding user response:

The reason for this is that DataService is returning an Object not an Array.

You can either use the keyvalue pipe and display it as an object:

  <div
    *ngFor="let item of dataArray | keyvalue" >
    <p>{{ item.key }} : {{ item.value }}</p>
  </div>

Or make sure dataArray is actually an Array and display it.

  <div
    *ngFor="let item of dataArray" >
    <p>{{ item }}</p>
  </div>

Also, you can click edit button below your question to edit it after posting

CodePudding user response:

0

{"patients":[{"_id":"6374db3de99156199a548845","firstname":"yassine","lastname":"azaiez","email":"[email protected]","age":23,"phone":92810192,"__v":0},{"_id":"6375045dd2eeac54c6ecb89b","firstname":"malik","lastname":"karim","email":"[email protected]","age":12,"phone":999999,"__v":0},{"_id":"637607be05816c5a7829130d","firstname":"alex","lastname":"karima","email":"[email protected]","age":12,"phone":999999,"__v":0},{"_id":"637607d105816c5a7829130f","firstname":"alex","lastname":"karima","email":"[email protected]","age":12,"phone":999999,"__v":0},{"_id":"63768732bc19c7bb581a24af","firstname":"malik","lastname":"azaiez","email":"[email protected]","age":18,"phone":97829101,"__v":0}],"user":null}

this is what I whant to display on my table except the element id I want to show firstname, lastname, email, age and phone

  • Related