[Vuejs]-Get result outside Vue istance

0👍

The callApiSearch method is performing an async operation; the then callback will be executed once the HTTP response is received which will occur after you accessed vueinstance.results.

I suggest you read up on JavaScript async concepts, promises, concurrency, etc so that you can understand this better. There’s plenty of resources online about this.

To fix it, make sure your callApiSearch function returns the promise:

callApiSearch: function (searchQuery) {
  return axios
    .post('api/search', searchQuery)
    .then(response => (this.results = response.data))
}

Then when you call the method, you can use then on the returned promise to execute code once the request has completed:

vueinstance
  .callApiSearch(searchQuery)
  .then(() => {
    console.log(vueinstance.results)
  })

Leave a comment