I can’t pass a value to a variable

Asked

Viewed 31 times

-1

Because in console.log(x) the terminal shows me the array and in console.log("Categories: " + this.Categories) it shows me Undefined?

buildCategories() {

    const teste = this.categoriesService.read();

    teste.subscribe(x => {
        this.categories = x;
        console.log(x);
    }
        );

    console.log("Categories: " + this.categories);

}
  • He prints the x in the first log, pq vc is printing exactly x. In the second log you are printing a variable that theoretically has x as value.

  • But I assigned the value of x to the Categories variable or I did wrong?

  • X is a right array? The variable Categories, is of type array??!

1 answer

3

This error has to do with the Subscribe being async so it doesn’t get stuck in it so the console.log that is out of subscription when it runs still has no value to var

Browser other questions tagged

You are not signed in. Login or sign up in order to post.