Answer the question
In order to leave comments, you need to log in
How to check if an action (mutation) called from another component has completed?
There is a SPA application with a chat (a system of dialogues linked to a specific article). In the main MainApp template, an action is called that initializes the initial chat data (the user's articles and whether there are unread messages for this article):
created() {
this.$store.dispatch('chat/InitChatSystem');
},
InitChatSystem() {
axios.get('/api/initChat')
.then(res => {
this.commit('chat/initArticles', res.data.articles);
this.commit('chat/initNotifications', res.data.notifications);
})
.catch(err => {
console.log(err);
})
},
initArticles(state, data) {
for (let i = 0, len = data.length; i < len; i++) {
state.articles.push(data[i]);
}
},
activated() {
this.$store.commit('chat/openConversationArticle', this.article_id);
},
openConversationArticle(state, article_id) {
....
//Ищется статья, чтобы пометить все сообщения как прочитанные
let notification_ids = [];
let article_index = state.articles.findIndex(x => +x.id === +article_id);
console.log(article_index); //Всегда -1 (т.е. не найдено)
state.articles[article_index].notification = false;
state.articles[article_index].notification_count = 0;
if (notification_ids.length > 0) {
axios.post('/api/markAsReadNotification', {notification_ids: notification_ids});
}
},
Cannot set property 'notification' of undefinedin this line state.articles[article_index].notification =
return new Promise((resolve, reject) => {
...
}
openConversationArticle({ dispatch, commit }) {
return dispatch('InitChatSystem').then(() => {
commit('openConversationArticle')
})
}
Answer the question
In order to leave comments, you need to log in
You don't need to check anything. What is the result of the completed action? - the availability of the necessary data. Make an appropriate getter, and hang it on a component instance v-if
with this getter. While there is no data, the component instance will not be rendered, which means that the mutation requiring the missing data will also not be called, which means there will be no error.
From the component, kick an Action that makes a request to the Beck. Action - a promise, if successful, makes a mutation if not, returns false. And in the component, you get the result of Action . And handles then/catch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question