Jump to content

TypeError: arrayOfObjects is not iterable


oslon
Go to solution Solved by Strider64,

Recommended Posts

TypeError: arrayOfObjects is not iterable
    at Object.<anonymous> (C:\Users\User\Desktop\100DAYOFCODE\1\1\script.js:3:19)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

The code in question:

const arrayOfObjects = require("./data");
for (const obj of arrayOfObjects) {
  console.log(`ID: ${obj.id}, Name: ${obj.name}`);
}

data is a file containing stuffs in this order

 

const arrayOfObjects = [
{ "id": 1, "name": "bla bla" },
{ "id": 2, "name": "bla bla bla" } 
];

Any ideas to resolve this issue?

Link to comment
Share on other sites

  • Solution
// Import the arrayOfObjects from the data module.
const arrayOfObjects = require("./data");

// Iterate through each object in the arrayOfObjects.
for (const obj of arrayOfObjects) {
  // Log the id and name properties of the current object to the console.
  console.log(`ID: ${obj.id}, Name: ${obj.name}`);
}

For the data module that contains the array of objects:

// Define an array of objects with properties id and name.
const arrayOfObjects = [
  { "id": 1, "name": "bla bla" },       // Object with id 1 and name 'bla bla'
  { "id": 2, "name": "bla bla bla" }   // Object with id 2 and name 'bla bla bla'
];

// Export the arrayOfObjects so it can be imported and used in other modules.
module.exports = arrayOfObjects;

 

Link to comment
Share on other sites

Because require() isn't like PHP where you "require" a file and then you get the variables in it. In Javascript, each file is like an independent module, so if you want to share - or export - data from a file then you need to use module.exports as Strider64 showed. What require() will then do, internally, is run the file and then return to you what was exported.

If you wanted to export just the one value then you can set module.exports like that. If you had multiple values then you can treat module.exports as an object (which is what it is by default) and set properties on it; this pairs well with destructuring.

const arrayOfObjects = [ ... ];

const anotherArrayOfObjects = [ ... ];

module.exports = { arrayOfObjects, anotherArrayOfObjects };
const { arrayOfObjects } = require("./data");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.