简体   繁体   English

TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]()

[英]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 actual array is this实际的数组是这个在此处输入图像描述

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.

相关问题 对不可迭代实例的解构尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method TypeError:解构不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 获取错误为“传播不可迭代实例的尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法' - Getting error as 'Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method' React js:传播不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - React js : Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance 解构不可迭代实例的无效尝试 - Invalid attempt to destructure non-iterable instance 未捕获的 TypeError:无效的解构不可迭代实例的尝试 - Uncaught TypeError: Invalid attempt to destructure non-iterable instance TypeError:对不可迭代实例 React/Jest 的解构尝试无效 - TypeError: Invalid attempt to destructure non-iterable instance React/Jest 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.' 解构不可迭代实例的无效尝试 - React Native - Invalid attempt to destructure non-iterable instance - React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM