0

am struggling to figure out, what's wrong with my code. I'm trying to call a rest API from my angular code, but it results in "TypeError: Cannot read property 'id' of undefined"

My clients.ervice.ts code looks like this:

import { Injectable } from '@angular/core';
import { Clients } from './clients.model';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable} from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class ClientsService {

  clientsUrl="http://localhost:21063/api/clints

  constructor(private http:HttpClient) { }
getAllClients():Observable<Clients[]>{
    return this.http.get<Clients[]>(this.clientsUrl);

}

My code in the client.component.ts

 constructor(private formBuilder: FormBuilder, private Service: ClientsService) { }
  clients:Clients[];
  ngOnInit() {

this.Service.getAllClients()
      .subscribe(data => this.clients=data);

and this is model.ts

export class Clients {
    id:number;
    name:string;
    phone:string;
    address:string;
    type:string;
    account:number;
    nots:string;
    branchId:number;
}

finaly my templets:

                  <tbody>
                        <tr *ngFor="let item of clients"></tr>
                        <td>{{item.id}} </td>
                        <td>{{item.name}} </td>
                        <td>{{item.phone}} </td>
                        <td>{{item.address}} </td>
                        <td>{{item.account}} </td>
                        <td>{{item.nots}} </td>

                    </tbody>

thanks in advance for any help to fix and understand this problem

1 Answer 1

3

No problem go easy on yourself, you forgot to make the tr element wrap all the td elements

<tbody>
  <tr *ngFor="let item of clients">
      <td>{{item.id}} </td>
      <td>{{item.name}} </td>
      <td>{{item.phone}} </td>
      <td>{{item.address}} </td>
      <td>{{item.account}} </td>
      <td>{{item.nots}} </td>
  </tr>
</tbody>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.