0

I have angular on the frontend and .net-core on the backend in my project. I simply use http get call for getting members array. I would like the list my members name. my code is like that;

<div *ngFor = "let member of members">
  <span>{{member.name}}</span>
</div>

But it doesn't work. My Http get works with status code 200 when I look at network tab on chrome's inspect screen but nothing show on the page fully blank. I tried to add extra code for detecting my error and editted my code like this;

<div>
  <span>{{members[0].name}}</span>
</div>
<div *ngFor = "let member of members">
  <span>{{member.name}}</span>
</div>

It works... I can show all members name on the page but have an error on console "cannot read property of member[0] of undefined"

Interesting problem, do you have any idea?

Btw, I tried add question mark to my array like <span>{{memeber[0]?.name}}</span> but still have same error. Actually I don't need this below part of the code;

<div>
  <span>{{members[0].name}}</span>
</div>

But I couldn't get members name on the page without this code-part.

I couldn't reach my codes now sorry. But I try to explain what am I did on my member.ts class;

1- I create memberModel and contains name, age, nationality, livingCountry 2- I post httpGet call to my backend and it retrive member infos from postgre db. 3- In member.ts class 3.1- I define empty members array. members : memberModel[]; 3.2- create a method getMembers() which subscribe to my http get method. 3.3- I call getMembers() on ngOnIni method.

6
  • 2
    Can you add the code how you assign value to members? And what that array contains? Commented Apr 7, 2021 at 7:02
  • I added the infos about .ts class to my question post Commented Apr 7, 2021 at 7:09
  • 1
    Just do a console.log(this.members) and show the result please. Commented Apr 7, 2021 at 7:09
  • 1
    It looks like you have a typo memebers Commented Apr 7, 2021 at 7:10
  • @Mikkel Christensen nope I did it now. Code doesn't have any typos on vs code. I editted Commented Apr 7, 2021 at 7:11

2 Answers 2

3

I guess your issue relies with the fetching of members. You said that you are getting this data from a .Net backend. Maybe your request is coming back later and this is why members is undefined when the component firstly loads. I would suggest you define the members array with a default value in the component.ts file:

public members = [];

Also, to further help, it would be very helpful to put more info, also the typescript file would help 😁.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. But as I said I didn't reach my codes now. Thx for your advice
1

Here is a whole working demo you could follow:

import { Component,Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  public members: memberModel[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<memberModel[]>(baseUrl +'weatherforecast/getmember').subscribe(result => {
      this.members = result;
    }, error => console.error(error));
  }
}
interface memberModel {
  name: string;
  age: number;
  nationality: string;
  livingCountry: string;
}

Backend Code:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet]
    [Route("getmember")]
    public IEnumerable<memberModel> getmember()
    {
        var model = new List<memberModel>()
         {
             new memberModel(){Age=12,Name="aa",Nationality="China",LivingCountry="ShangHai"},
             new memberModel(){Age=25,Name="bb",Nationality="USA",LivingCountry="Washington"},
             new memberModel(){Age=32,Name="cc",Nationality="UK",LivingCountry="London"}
         };
        return model;
    }
}

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.