Skip to content
Advertisement

Problems using values after axios

I am sorry if this problem is already solved somewhere but I can’t wrap my head around what my problem is. In my scenario I want to make 2 axios calls and do some stuff with the two data responses before the page is finally rendered. If I output the data in my template it is visible but when I want to use it before the page is rendered the value is always undefined. After some research I came up with the following solution:

JavaScript

If I want to do something with these values for example pass it to another value it wont work because Stuff1 is undefined. Why is that the case? In my understanding the async functions should wait until the promise is finished because of await so the value should exist after the 2 awaits in getStuff() but this is not the case. I am really thankful for any help!

Edit I tried both solutions mentioned but getting the same mistake. For clearness I added the whole code.

JavaScript

Advertisement

Answer

I think you should use a promise as so this means once the axios call is done you fill the data and not before it happens I saw that in you edit u added console.log in created that doesn’t work for sure because it happens before the data is fetched in the code i offered i added v-if on the table that way you will avoid any error in the console for rendering this before the data is fetched, I used mounted instead of created because it is called after DOM has been mounted, this should work and let me know if you have any problems

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement