Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to element of array is undefined #318

Closed
mangeleptico opened this issue Jun 16, 2021 · 3 comments
Closed

Access to element of array is undefined #318

mangeleptico opened this issue Jun 16, 2021 · 3 comments

Comments

@mangeleptico
Copy link

When i try to access to any element of authors array, the response is undefined, only works when i change to books table and then return to authors. This happends with all get a collection of resources

example:
` public authors: DocumentCollection;

public constructor(private route: ActivatedRoute, private authorsService: AuthorsService, booksService: BooksService) {
    route.queryParams.subscribe(({ page }) => {
        authorsService
            .all({
                include: ['books'],
                sort: ['name'],
                page: { number: page || 1 },
                ttl: 3600
            })
            .subscribe(
                authors => {
                    this.authors = authors;
                },
                error => console.error('Could not load authors :(', error)
            );
    });
}

ngOnInit(): void  {
     console.log(this.authors.data); // No problem
    console.log(this.authors.data[0]); // Here the value is undefined
}`
@pandabamboo90
Copy link

pandabamboo90 commented Jun 17, 2021

@mangeleptico

I had the same problem before :)
You can use filter operator from RxJS to get the resources when it has been "built & loaded" before assign it to your data variable

    authorsService
      .all({
        include: ['books'],
        sort: ['name'],
        page: { number: page || 1 },
        ttl: 3600
      })
      .pipe(
        filter(authors => authors.loaded), // Only get the response when every resources are loaded !
      )
      .subscribe(
        authors => {
          this.authors = authors;
        },
        error => console.error('Could not load authors :(', error)
      );

@mangeleptico
Copy link
Author

Thanks! its works for me.
It's necessary too use a conditional of authors in html if i want to print authors data on screen to avoid asynchronous errors

@niskah-energies
Copy link

Hi,

If I try to user filter(), I have an error:

TypeError: filter is not a function. (In 'filter(function (authors) { return authors.loaded; })', 'filter' is undefined)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants