2

How can we get data from an array of objects in angular. I have provided the typescript & service file & the consoled image. How can we able to get datas inside results object in the image??

Ts File

this.commonService.fetchVersionValue().subscribe((data) => {
      console.warn(data);
    });

Service File

fetchVersionValue(): Observable<IVersionDetails[]> {
    return this.http.get<IVersionDetails[]>(
      `${this.PHP_API_SERVER}/api/get/versions`
    );
  }

Image

enter image description here

while looking the console I got the error enter image description here

The Api from php Server Help Me to Sort this Problem . Thanks in Advance

1
  • This is because you are passing an object literal instead of an iterable object (array) to *ngFor directive. The property that you bind to the template from the typescript file is not pointing to the data key in the API endpoint response in the screenshot. in the subscribe do .subscribe((response)=>console.log(response['data'])) that will solve your issue accessing the objects inside the array called data correctly. Assign this response['data'] to a property and pass it to the *ngFor. Commented Jun 2, 2022 at 11:55

3 Answers 3

1

this.commonService.fetchVersionValue().subscribe((response: any) => {
      console.warn(response.results.data);
    });

This Works !!!

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

Comments

0

Your version details values are stored in the data property in the returned response. So you need to iterate over that property values.

this.commonService.fetchVersionValue().subscribe((response) => {
   console.warn(response.data); // Renaming to response for clarity
});

1 Comment

thanks in advance bro . i check but i got like this Property 'data' does not exist on type 'IVersionDetails[]'.ts(2339)
0

This is because you are passing an object literal instead of an iterable object (array) to *ngFor directive. The property that you bind to the template from the typescript file is not pointing to the data key in the API endpoint response in the screenshot. In the subscribe do subscribe((response)=>console.log(response['data'])) that will solve your issue accessing the objects inside the array called data correctly. Assign this response['data'] to a property and pass it to the *ngFor. –

3 Comments

Thanks walid .i tried like this but i got Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
That's TypeScript compiler complains about the way you define your property type. You can set it explicitly to any (I do not recommend this) because it is against type safety which is why we use TypeScript instead of JavaScript. Or you have to define the property correctly by either passing an object instead of an array something like this -property: { [key: string]: yourInterfaceName [] } = {}. Or you let it to the compiler to infer the type by itself from the context. Changing tsconfig.ts configuration to disable warnings is not good option.
If none of the above worked , please provide a stackblitz link with how you define your properties in the ts file.

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.