1

currently I stuck on a problem for several hours. Sadly I haven't found a working solution on this by googling around the world.

The problem is, that I cant't access the "self" or "this" variable inside the same function after pulling the data from an api. I am just getting an 'undefined'.

getInvoice(){
  var self = this
  axios
    .get(url)
    .then(response => (self.invoice = response.data.data))

  console.log(self.invoice.invoice_nr) //undefined
},

Best regards and thanks in advance.

Alex
  • 33
  • 2

1 Answers1

1

Firstly, this is not Vue JS issue.

Rest APIs are asynchronous in Javascript regardless of the framework you use.

Now, In order to access the data from Promise, you need to add it inside the .then function.

Have a look at Axios Docs

Promises

To get your data,

getInvoice(){
  var self = this
  axios
    .get(url)
    .then(response => {
        self.invoice = response.data.data;
        console.log(self.invoice.invoice_nr);
    })
},

Hope this helps!

Varit J Patel
  • 3,497
  • 1
  • 13
  • 21