0

I'm trying to fetch an array of data from Mongoose, loop through the array and add an object to Three.js scene for each item in array.

When I try to render the scene in the browser I'm getting an error saying:

"client_world.js:37 Uncaught (in promise) TypeError: data is not iterable at getData"

However when I console log the data in the get request querying the database, it prints all of the data entries as an array, sp they must be iterable.

I think maybe the data is not arriving to the javascript file.

Here is the code to get the data from the db:

const indexVR = (req, res) => {
    Upload.find({})
    // .populate('Upload')
    .exec(function (err, docs) { 
        if (err) res.send(err)
        // docs.toString()
        res.json(docs)
        console.log(docs)
    })
};

this function is in the following get req:

router.get('/indexvr', FileCtrl.indexVR);

This is the code snippet from the js file:

getData()

async function getData() {
  const response = await fetch('/api/indexvr');
  const data = await response.json;
  console.log(data)

   for (item of data) {
    try {
     scene.add( cube );
    } catch (e) {
     console.error(e)
}
  }
}   

nothing in logging to the console in the getData function, however the browser is reading some kind of empty json object.

.. Does anyone know what I'm doing wrong?

1 Answer 1

1

Your issue may be as simple as calling the .json method as opposed to referencing it.

Referring to this document: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch .json is a method of the response object returned from fetch.

Try using const data = await response.json();

Sign up to request clarification or add additional context in comments.

1 Comment

You're right, thank you very much! Probably would've taken me days to figure that out as I would've thought it was something deeper!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.