简体   繁体   中英

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. and I want to get the list of Array which has objects. 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

TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator

this is my code that gets data from firebase real-time database

 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;

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

     mounted() {
    read_project.getAllProject();
  },

the output error is this

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 . 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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM