Error: Trying to destructure a non-iterable instance is not valid. Non-array objects must implement a [Symbol.iterator]() in order to be iterable

Greetings to all who come across this query. I am currently working with Vue 2 and Firebase, aiming to retrieve a list of Arrays containing objects. I have successfully fetched the list from the real-time database in Firebase, but encountered an issue when attempting to store this array into the Vuex state.

Error Message: TypeError: Invalid attempt to destructure non-iterable instance. Non-array objects must be iterable with Symbol.iterator

Here is my code for retrieving data from the 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(DataSnapshot) {
          try {
            store.dispatch("projects/set_project", DataSnapshot.val());
          } catch (error) {
            console.log(error);
          }
        });
      }
    });
  }

Below is my Vuex code snippet:


export const namespaced = true;

export const state = {
  test: []
};

export const mutations = {
  SET_PROJECT(state, payload) {
    state.test.push(payload);
  }
};

export const actions = {
  set_project({commit}, payload) {
    commit("SET_PROJECT", payload);
  }
};

This is where I call the getAllProject method:

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

The error message output is as follows:

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_project (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)

Actual array snapshot: https://i.sstatic.net/gX0Wy.png

Answer №1

The issue lies within the following lines of code:

set_poject([commit], paylod) {
    commit("SET_PROJECT", paylod);
  }

Essentially, the commit is contained within an object, and attempting to destructure it as an array is not possible. This results in the error message "destructure non-iterable instance."

To resolve this problem, adjust the code as follows:

set_poject({commit}, paylod) {
    commit("SET_PROJECT", paylod);
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resolved: Angular JS resolves circular dependencies

I encountered a Circular dependency issue while attempting to make a http.post call in a http interceptor: Circular dependency found: $http <- Ace <- authInterceptor <- $http <- $templateRequest <- $compile I understand the problem, but I ...

Ways to transfer the value of a JavaScript variable to a PHP variable

Similar Question: How can I transfer JavaScript variables to PHP? I am struggling to assign a JavaScript variable to a PHP variable. $msg = "<script>document.write(message)</script>"; $f = new FacebookPost; $f->message = $msg; Unfort ...

Middleware functions in Mongoose for pre and post actions are not being triggered when attempting to save

After carefully reviewing the documentation, I am still unable to pinpoint the issue. The pre & post middleware functions do not appear to be functioning as expected. I have made sure to update both my node version and all modules. // schema.js const sch ...

What is the best way to find a specific string within an array of strings?

I have a list of tasks as strings: todo=[ 'Get up', 'Brush my teeth', 'Go to work', 'Play games' ]; I am attempting to compare it with this: Template: <input (input)="checkArrays($event)" /> In my ...

Pagination Component for React Material-UI Table

I am interested in learning about Table Pagination in React UI Material. Currently, my goal is to retrieve and display data from an API in a Material UI Table. While I have successfully implemented some data from the API into the Material UI Table, I am ...

Manipulate SVG using Javascript

Is there a way to edit an SVG file directly in JavaScript without relying on any framework? I have a master SVG file that contains some child SVG elements. I've managed to retrieve the content of these children using Ajax, but now I need to insert t ...

What sets React$Element apart from ReactElement?

Attempting to implement flow with ReactJS and needing to specify types for prop children. The article on Flow + React does not provide any information. Despite encountering examples using ReactElement (e.g), I received an error message from flow stating i ...

Does the AngularJS inject function operate synchronously?

Does the AngularJS inject method work synchronously? Here is an example: inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; rootScope = _$rootScope_.$new(); }); ...

Conceal the div upon clicking anywhere on the page, except if a particular div is clicked

Whenever the user interacts with topic_tag_sug or any of its child elements, the div should remain visible. However, if the user clicks on any other element, topic_tag_sug should be hidden. HTML <input id='topic_tag' onblur="$('#topic_t ...

An alternative method to confirm the checkbox selection without physically clicking on it

Currently, I'm in the process of creating a basic to-do list and have been attempting to connect the task with its corresponding checkbox. My goal is for the checkbox to automatically be checked when the task is clicked. Instead of using HTML to add ...

The JSON Request functionality of jQuery

Having some trouble with my AJAX request to a php file. The goal is to get a JSON object as response, but for some reason, the AJAX function keeps failing when I specify that I want JSON returned. Any suggestions or advice would be greatly appreciated. Th ...

Keep the music playing by turning the page and using Amplitude.js to continue the song

I have implemented Amplitude.js to play dynamic songs using a server-side php script. My objective is to determine if a song is currently playing, and if so, before navigating away from the page, save the song's index and current position (in percenta ...

Issue with loading glb file in three.js: The 'format' property is not compatible with this material

When loading a .glb file I created in Blender using three.js, I am encountering an error message three.module.js:7950 THREE.MeshStandardMaterial: 'format' is not a property of this material.. The rest of the content loads correctly. What does thi ...

When the Protractor function is invoked from a Cucumber step definition, it mysteriously returns undefined, despite the fact that the console.log()

Currently, I am in the process of developing an Automation Framework using Protractor-cucumber for the organization I work for. The framework includes several libraries that play crucial roles in its functionality: Protractor Cucumber-js Gulp Chai Chai-a ...

Receiving information within an Angular Component (Profile page)

I am currently developing a MEAN Stack application and have successfully implemented authentication and authorization using jWt. Everything is working smoothly, but I am encountering an issue with retrieving user data in the Profile page component. Here ar ...

How can I change the color of a designated column in a Google stacked bar chart by clicking a button?

I am in the process of creating a website that compares incoming students at my university across different academic years. We have successfully implemented a pie chart to display data for the selected year. Now, we aim to include a stacked bar chart next ...

Utilize $.ajax to gracefully wait for completion without causing the UI to freeze

Consider a scenario where there is a JavaScript function that returns a boolean value: function UpdateUserInSession(target, user) { var data = { "jsonUser": JSON.stringify(user) }; $.ajax({ type: "POST", url: target, data: ...

Retrieve data from Instagram authentication

After successfully using client-side Instagram authentication, I am granted an access token that allows me to access user media: https://instagram.com/oauth/authorize/?client_id=#####&redirect_uri=http..&response_type=token' Unfortunately, u ...

Modifying pagination numbers with Reactjs: A step-by-step guide

I am currently working on Reactjs (nextjs) and I have successfully integrated the "Nextjs" framework. The pagination is working fine, but the buttons are displaying as "1,2,3,20" instead of "1,2...20" (showing all numbers without using "..."). How can I mo ...

Ensure that scripts with the type of "module" are completed before proceeding to execute the following script

In my HTML document, I have two script tags with type="module" at the bottom of the body tag. Following those, there is another script tag with embedded code that relies on the results of the first two scripts. Is there a method to ensure that this code ...