[英]TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]()
Hello to those who read this issue.阅读此问题的人您好。 I using Vue 2 with firebase.
我将 Vue 2 与 firebase 一起使用。 and I want to get the list of Array which has objects.
我想获取包含对象的 Array 列表。 the list gets successfully from the firebase real-time database but the issue is that when I want to store this array into the Vuex state I got this error
该列表从 firebase 实时数据库中成功获取,但问题是当我想将此数组存储到 Vuex state 时出现此错误
TypeError: Invalid attempt to destructure non-iterable instance. TypeError:无效的解构不可迭代实例的尝试。 In order to be iterable, non-array objects must have a Symbol.iterator
为了可迭代,非数组对象必须有一个 Symbol.iterator
this is my code that gets data from firebase real-time database这是我从 firebase 实时数据库中获取数据的代码
getAllProject() {
//var result = [];
var userId = store.state.user.user.uid;
var project_ref = database.ref("projects/" + userId + "/");
project_ref.on("value", function(snapshot) {
if (snapshot.hasChildren) {
snapshot.forEach(function(DataSnapsho) {
try {
store.dispatch("projects/set_poject", DataSnapsho.val());
} catch (error) {
console.log(error);
}
});
}
});
}
this my Vuex code export const namespaced = true;这是我的 Vuex 代码 export const namespaced = true;
export const state = {
test: []
};
export const mutations = {
SET_PROJECT(state, paylod) {
state.test.push(paylod);
}
};
export const actions = {
set_poject([commit], paylod) {
commit("SET_PROJECT", paylod);
}
};
this is the place I invoke the getAllProject
methods这是我调用
getAllProject
方法的地方
mounted() {
read_project.getAllProject();
},
the output error is this output 错误是这样的
TypeError: Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
at _nonIterableRest (nonIterableRest.js?3d8c:2)
at _slicedToArray (slicedToArray.js?3835:6)
at Store.set_poject (projects.js?f817:296)
at Array.wrappedActionHandler (vuex.esm.js?2f62:851)
at Store.dispatch (vuex.esm.js?2f62:516)
at Store.boundDispatch [as dispatch] (vuex.esm.js?2f62:406)
at eval (readProject.js?72b9:18)
at eval (index.esm.js?e947:4417)
at LLRBNode.inorderTraversal (index.esm.js?e947:2769)
at SortedMap.inorderTraversal (index.esm.js?e947:3219)
The problem is with your this lines of code:问题在于您的这行代码:
set_poject([commit], paylod) {
commit("SET_PROJECT", paylod);
}
Actually the commit
is inside object, and you cannot destructure object as array .实际上,
commit
在 object 内部,您不能将 object 解构为数组。 So when you try to do that, it gives the error destructure non-iterable instance.因此,当您尝试这样做时,它会给出错误解构不可迭代实例。
Update the code like this:像这样更新代码:
set_poject({commit}, paylod) {
commit("SET_PROJECT", paylod);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.