Need advice: axios call needed in computed values

i don’t have any code, because the code i have is ‘working’, meaning, it generates the values i need, but not in the time i need them. allow me to explain:

i have one calculated value that creates an array of ids and returns the length of the array.

i need to now take that created array of ids and query the database to fetch a count of records that match the ids in the array AND an additional criteria.

as the database gets updated through the functioning of app, i need to recalculate those matches.

my want is to use a calculated value, but i know that they don’t wait for axios returns. so my question is, how do i solve this problem? would a watch be a better solution in this scenario?

why not to use a method?
Call it when you need to update the value.

i thought about this. in fact, this is where i started. the problem was that the app didn’t update when the method returned the values. perhaps it was because i was calling it inline, like so: {{ updater() }}?

You just need to use the variable for which value is being set computed value will observe the change and will change its value

hm. so calling the function inline is not the way to go. i thought it would basically be a v-bind when done that way. ok. thank yoiu. have to rethink this…

Definitely don’t do this. Computed properties should never have side effects, e.g. an axios request.

As others have said, use a method to do the call and set the returned data. Use a watcher to trigger this based on your computed array.

okay. i think i’m almost there. can i watch any value and have a method update a different value? my presumption is yes, in which case that’s the bunny i’m looking for.

Yes. As long as it’s a reactive value (i.e. with an observer on it. e.g. data, computed prop, prop) then you can watch it for changes.

@JamesThomson Yes! that’s what i’m looking for. excellent. while this may have seemed pointless, it has helped me a lot coming to grips with how vuejs behaves and, also, a lot more on how JS itself behaves. thank you. very much appreciated. :slight_smile:

No worries. Nothing is pointless as long as you learn from it :wink:

Hi I typically put a call to my Get Items method in the Created hook, and then assign to data items using this.. Remember to use await in your call to axios assuming you are going an asynchronous call. Paul