[Vuejs]-How to make subsequent axios call for the array result of the first axios call?

0👍

This should work, couldn’t test it. The code could be a little DRY’d up.

async getOffTheJobs(id, token) {
    const self = this
    const instance = axios.create();
    delete instance.defaults.headers.common['X-CSRF-TOKEN'];

    const opts = {
        headers: { 'Authorization': token }
    }
    const url = `${initialState.offTheJobApiBase}offthejob/${id}`
    const { data } = await instance.get(url, opts)

    if (!(data.status && data.status == 'success' && data.data)) {
        self.show = true
        return
    }

    // Calculating total hours
    const { activities } = resp.data
    const p = activities.map(async (offTheJobAct) => {
        self.totalOffTheJobActivityHours += offTheJobAct.totalHours;
        if (offTheJobAct._id) {
            try {
                const url = `${initialState.offTheJobApiBase}activity/${offTheJobAct._id}`
                const { subData } = await instance.get(url, opts)
                if (subData.status && subData.status == 'success' && subData.data && 
                    !self.isEmptyArrayObject(subData.data.activityEvidences)) {
                    self.offTheJobEvidences[offTheJobAct._id] = subData.data.activityEvidences
                }
            } catch (err) {
                console.warn(err.response.data.error)
            }
        }
    })

    await Promise.all(p)
    self.offTheJobDetails = data.data
    self.show = true
},

Leave a comment