0

I can't seem to get async/await working properly in my Vue JS app. I'm firing it in the "created" lifecycle hook but when I console.log out the result in the "beforeMount" lifecycle hook, I get null.

I need the variables holidays and holidaysMap to be populated before the DOM loads but can't seem to do this.

async beforeCreate() {
    try {
        this.holidays = await axios({
        method: 'post',
        url: 'https://controlapi.totalprocessing.com/api/get-holiday',
        data: { userId: localStorage.user_id },
        config: { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}
        })
        .then(response => {
            return response.data;
        })
        .catch(e => {
            this.errors.push(e)
        })

        this.holidays.forEach(e => (this.holidaysMap[e.date] = this.holidaysMap[e.date] || []).push(e));
        console.log(this.holidaysMap);
    }
    catch(e) {
        console.log(e)
    }
},
beforeMount() {
    console.log(this.holidays);
}

enter image description here

<v-calendar ref="calendar" v-model="today" @click:date="dayClick" :value="today" :end="end" :weekdays="weekdays" :locale="locale" :type="type" :short-months="false" color="primary">
    <template v-slot:day="{ date }">
        <template v-for="holiday in holidaysMap[date]">
            <v-menu :key="holiday.first_name" v-model="holiday.open" full-width offset-x>
                <template v-slot:activator="{ on }">
                    <div v-if="!holiday.time" :class="{ 'half-day-1' : holiday.half_day == 'AM', 'half-day-2' : holiday.half_day == 'PM' }" v-ripple class="event" v-on="on" v-html="holiday.first_name"></div>
                </template>
                <v-card color="white" min-width="350px" flat>
                    <v-toolbar color="primary" dark>
                        <v-toolbar-title v-html="holiday.first_name"></v-toolbar-title>
                    </v-toolbar>
                    <v-card-title primary-title>
                        <div class="holiday-details-container">
                            <span class="holiday-details-title">Date:</span><span class="holiday-details">{{ holiday.date }}</span><br>

                            <span class="holiday-details-title">Description:</span><span class="holiday-details">{{ holiday.description }}</span><br>

                            <span v-if="holiday.half_day" class="holiday-details-title">Half Day:</span>
                            <span v-if="holiday.half_day == 'AM'" class="holiday-details">Morning</span>
                            <span v-if="holiday.half_day == 'PM'" class="holiday-details">Afternoon</span><br>
                        </div>         
                    </v-card-title>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="error">Delete</v-btn>
                        <v-btn color="primary">Ok</v-btn>
                    </v-card-actions>
                </v-card>
            </v-menu>
        </template>
    </template>
</v-calendar>
Dally
  • 1,281
  • 4
  • 18
  • 37
  • You're using async/await correctly, but Vue.js doesn't `await` (or consume in any way) the Promise you're returning from beforeCreate. There is nothing you can do about that. There is a fair bit of discussion about it here: https://github.com/vuejs/vue/issues/7209 – Paul Jul 18 '19 at 19:56
  • @Paulpro - Is there a work around for this? How can I make the DOM wait without throwing an error? It's the 3rd line... – Dally Jul 18 '19 at 19:58
  • I don't really know Vue.js so I'm probably not going to be much help, but you can get rid of the error by making sure that `holidaysMap[date]` is an empty array synchronously (before awaiting anything in beforeCreate). or maybe you can change the line above to ` – Paul Jul 18 '19 at 20:05

0 Answers0