【发布时间】:2021-05-06 04:16:12
【问题描述】:
我无法使从 firebase 获取数据的函数异步:
当组件被创建时,我有:
created(){
this.$store.dispatch("fetchSections");
}
Vuex 动作:
actions: {
async fetchSections({ commit }) {
var sectionsRef = db.collection("sections");
try {
await sectionsRef.get().then((querySnapshot) => {
var collect = querySnapshot.docs.map((doc) => doc.data());
commit("addToSections", collect);
});
} catch (error) {
console.log(error.message + " -- " + error.statusText);
}
},
}
问题:在我们获取数据之前创建了另一个组件。
我也试过这个:
actions: {
async fetchSections({ commit }) {
var sectionsRef = db.collection("sections");
try {
let allSections = await sectionsRef.get();
for (const doc of allSections.docs) {
var collect = doc.data();
commit("addToSections", collect);
}
} catch (error) {
console.log(error.message + " -- " + error.statusText);
}
},
},
【问题讨论】:
标签: javascript vue.js async-await vue-component vuex