I've been grappling with this issue for some time now. I understand how Nuxt asyncData can be used to make either one request or multiple requests, but is there a way to incorporate conditional logic for making multiple requests based on a query parameter? For instance, sometimes it should make one request and other times two requests, depending on the query param.
async asyncData({ $axios, route }) {
if (route.query.query) {
if (route.query.tab === 'companies') {
// API Call "Search companies"
return await $axios.$get(`/v1/search/companies/${route.query.query}`).then((res) => {
return {
tab: 'companies',
query: route.query.query,
companyResults: res.data.data,
}
})
} else {
let company = null
if (route.query.company) {
// API Call "Read a company"
await $axios.$get(`/v1/companies/${route.query.company}`).then((res) => {
company = res.data
})
}
// API Call "Search requests"
return await $axios.$get(`/v1/search/requests/${route.query.query}`).then((res) => {
return {
company,
tab: 'requests',
query: route.query.query,
requestResults: res.data.data,
}
})
}
}
}
The issue lies within the else
statement. When the "company" query param is set, I want it to trigger both the "Read a company" and "Search requests" API calls. However, if the query parameters are not set, then only the "Search requests" API call should be made.
Although all the correct API calls are being made when I run this code, the data is not being returned correctly from asyncData.