How to take the value of the variable within a Function in IONIC?

Asked

Viewed 87 times

0

  getListTodayOrders(): InterfaceOrders {
    const url = 'http://oneqbackend.herokuapp.com/api/listas';
    this.http.get(url).subscribe((data: InterfaceOrders) => {
        this.data_return = data;
        console.log(' Valor trabalha bem aqui ==> ' , this.data_return);
      });
      console.log(' Aqui o valor desaparece ==> ' , this.data_return);
      return this.data_return;
  }

Inspect:

Value works well here ==> (6) [{... }, {...}, {...}, {...}, {...}, {...}, {...}]
Here the value disappears ==> Undefined

  • 1

    Where he declared data_return?

  • 3

    The problem is that subscribe is asynchronous, that is, when you run the function to receive the value it already passes to the line below, and when it finishes fetching the information in the url it assigns the value.

  • right. solved. thanks staff.

No answers

Browser other questions tagged

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