1

It's supposed to display the categories list data from the database of the backend into a table in the frontend. but, no data has been displayed yet, because of this error.

categories-list.component.ts :

import { Component, OnInit } from '@angular/core';
import { CategoriesService, Category } from '@bluebits/products';

@Component({
  selector: 'admin-categories-list',
  templateUrl: './categories-list.component.html',
  styles: []
})
export class CategoriesListComponent implements OnInit {

  categories: Category[] = []

  constructor(private categoriesService: CategoriesService) { }

  private getCategories() {
    this.categoriesService.getCategories().subscribe( (data:any) => {
      this.categories = data.categories
      console.log(data)
    })
  }

  ngOnInit(): void {
    this.getCategories()
  }
}

categories.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Category } from '../models/category';

@Injectable({
  providedIn: 'root'
})

export class CategoriesService {

  apiMainUrl= "http://localhost:3000/api/v1"

  constructor(private http: HttpClient) { }

  getCategories(): Observable<Category[]> {
    return this.http.get<Category[]>(`${this.apiMainUrl}/categories`)
  }
}

categories-list.html

 <ng-template pTemplate="body" *ngFor="let category of categories" let-category>
        <tr>
            <td>{{category.id}}</td>
            <td>{{category.name}}</td>
            <td>{{category.icon}}</td>
        </tr>
 </ng-template>
11
  • Can you give the exact line at which the error appears? Commented Nov 8, 2021 at 23:12
  • try data.body.categories Commented Nov 8, 2021 at 23:45
  • @Nicho it didn't work :\ Commented Nov 9, 2021 at 8:21
  • 1
    @EslamGohar you should have a log in your console with the exact line of this error. Please copy paste it and indicate at which line it's failing... Your error largely has to do with trying to read a property of an undefined object, which for example here would be category. It would help a lot to know where the error comes from... Commented Nov 9, 2021 at 8:43
  • 1
    @RebaiAhmed Thanks bro ♥ though I have tried more and it wasn't being worked. but, Finally, It's worked now :D Commented Nov 9, 2021 at 22:01

2 Answers 2

1

Looks like your using PrimeNG for your table. I don't think you use ngFor on the ng-template element. I think you need to remove ngFor and cahnge the iterator should be let-categories this in the template you can't access each item with categories.id.

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

2 Comments

I have removed ngFor and changed the iterator as you said. but, I couldn't get the correct result which displays the categories data on the table
Are you getting back? like the console.log? If so try piping the 'categories' into the template
0

I have finally solved this issue. It was in the getCategories() function, which should be call:

this.categories = data.data```

NOT

this.categories = data.categories

So that the array of data can be displayed on a table in the frontend.

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.