How to start fetch only when two variables in watch true?
I need to call fetch only when both values are available (not undefined)
2 Replies
Hi @salomon_.. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on your code, you're trying to fetch data only when both
date.value.value and direction.value.value are available. There are a few approaches to handle this in Nuxt 3.
Option 1: Use a computed property
You can create a computed property that checks if both values are defined and use that in your watch array:
Option 2: Use the immediate: false option with manual execution
You're already using immediate: false, which prevents the request from firing immediately. You can then manually trigger the fetch when both values are available:
This approach uses Vue's standard watch function to monitor both values and only executes the fetch when both are defined Docs > Getting-started > Data Fetching > Watch.
Option 3: Use a computed URL
Another approach is to use a computed URL that returns undefined when the values aren't available:
This approach leverages the Computed URL feature of useFetch and useLazyFetch.
The second option is probably the most straightforward for your specific use case, as it gives you explicit control over when the fetch is executed.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1029431292170874910> Kapa.ai is still learning and improving, please let me know how I did by reacting below.