Error message: "The jsonLoader in threeJS is throwing an error: 'Cannot retrieve the length property of undefined.'"

I am in the process of creating a dynamic scene that can be saved and then loaded at a later time.

When saving the scene, I serialize all objects along with their data. The process for meshes is quite straightforward:

var json = {};
json.objectsInScene = [];
for (var i = 0; i < collectionOfObjects.length; i++)
{
 
    json.objectsInScene.push(collectionOfObjects[i].toJSON();
}

Upon loading the scene back in, this is how it is approached:

var jsonObject = JSON.parse(json);

for (var i = 0; i < jsonObject.objectsInScene.length;i++){
    
    var loader = new THREE.JSONLoader();
    var geometry = loader.parse(jsonObject.objectsInScene[i]);

    var mesh = new THREE.Mesh(geometry.geometry, new THREE.MeshBasicMaterial());

    this.scene.add(mesh);
}

An error occurs when executing the code:

Error: Cannot read property 'length' of undefined

Located at Line 14990 of three.js.

The problematic line is identified as follows:

zLength = vertices.length;

Further investigation reveals that vertices.length is indeed undefined. It appears the assignment takes place a few lines earlier:

var vertices = json.vertices;

However, the json object only consists of the following properties:

geometries: Array[15]
materials: Array[15]
metadata: Object
object: Object

What could be the issue here? Where does the problem arise, and what steps can be taken to resolve it?

R73

Answer №1

Consider utilizing THREE.ObjectLoader for this task.

for (let i = 0; i < jsonObject.objectsInScene.length; i++){

    let loader = new THREE.ObjectLoader();
    let obj = loader.parse(jsonObject.objectsInScene[i]);

    this.scene.add(obj);
}

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

What is the best method for retrieving environment variables within a React JS web application?

Within my render method, I am trying to access a specific environment variable. However, because it is a custom ENV variable, I cannot utilize (process.env.NODE_ENV). According to this source, React filters all process.env access. What would be the best w ...

Issue with jquery's .load() and getScript functions

Earlier today, I encountered an issue with a .load() function being called in a script. I managed to solve the problem using .getScript(), but now I'm facing a major issue - the function is being executed multiple times. You can find the full code a ...

Fixed positioning upon scrolling begins prior to reaching the uppermost point (top div)

Currently, I have implemented a feature where #filter (the white select list in my sidebar) becomes fixed when it reaches the top of the page and stays there while scrolling until it reaches the footer and is released. However, I encountered an issue wit ...

Displaying imported JSON data in a tabular format

I am struggling with writing a function in my VueJS template that sends a request to the backend API with a specific key and then displays the JSON response in a table. For example, here is a sample of the JSON data: {"driver_id":1,"driver_name":"{driver ...

Is there a way to eliminate unnecessary chunks from MUI in NextJS?

https://i.sstatic.net/zAkV0.png I recently created a single page and noticed that there is a significant amount of unused JavaScript in my codebase. Specifically, I am using MUI and React icons. Any suggestions on how to efficiently manage this issue? T ...

Creating a radial progress chart using Plotly JavaScript

I recently started working with the Plotly library and now I need to display a circular progress graph in Vuejs 2, like the one shown below. While Plotly is a comprehensive tool, I have not come across an example that matches this specific design using Ja ...

Accepting POST requests from an external source in AngularJS

I am currently working on an application that utilizes AngularJS 1.4.0 and requires the ability to receive POST data from an external source. In AngularJS, routes often use parameters in the URL format like this: .when('/section/:param', { t ...

Obtaining an array of weeks for the previous two months from the current date using JavaScript

Looking to generate a list of weeks for the past two months using JavaScript. Any suggestions on how to accomplish this task would be greatly appreciated. [ { "week_start": "8/01/2016", "week_end": "8/06/2016" }, { "w ...

Verify the definition of the checkbox

I'm attempting to determine whether a checkbox is defined and unchecked. When the page loads, my checkbox is disabled and I receive an error indicating that it is undefined. I attempted to create a condition to prevent the error but was unsuccessful. ...

Exploring MongoDB's dynamic Child Object access functionality in a PHP environment

How to access dynamic child objects in MongoDb using PHP In the example code below, I need to perform the same query in PHP. To get the result in MongoDb, you can use the following SHELL Script: db.getCollection('Data').find({'COLLECTION. ...

Using localStorage in Next.js, Redux, and TypeScript may lead to errors as it is not defined

Currently, I am encountering an issue in my project where I am receiving a ReferenceError: localStorage is not defined. The technologies I am using for this project are Nextjs, Redux, and Typescript. https://i.stack.imgur.com/6l3vs.png I have declared ...

What is the best way to arrange the keys within a nested object in JavaScript?

Question: { "foo": "bar", "bar": "baz", "baz" : { "nestedKey": "foo" } } In order to sign this request using the Hmac512 algorithm, I must first stringify the object. I am concerned that if the key order is not preserved, the generated signature on the ...

How can I substitute a specific capture group instead of the entire match using a regular expression?

I'm struggling with the following code snippet: let x = "the *quick* brown fox"; let y = x.replace(/[^\\](\*)(.*)(\*)/g, "<strong>$2</strong>"); console.log(y); This piece of code replaces *quick* with <strong& ...

I used npm to install AngularJS and then included AngularJS in my application

My goal is to set up AngularJS v1.5.x using npm and integrate it into my application for seamless utilization. Most tutorials opt for downloading the Angular Version from angularjs.org and manually adding it to the index.html within a <script></sc ...

Tips for ensuring all data is downloaded in Firebase (Firestore) Storage before proceeding?

I am currently developing a light dashboard in Vue that connects to Firestore and Storage. As someone who is not an expert, I have encountered a roadblock in what should be a simple task. The issue lies with a function that is meant to retrieve all URLs ba ...

Error Message: TypeError - Unable to access property 'method' as it is not defined

I've just started working on a new node project and I've encountered an issue that I can't seem to figure out. :\Users\RTECH\Desktop\work\aumentedreality\modelViewerwithExpress\node_modules\express&b ...

Tips for conducting unit tests on navigator.notification.alert

I am currently facing a challenge in writing a unit test for a JavaScript function where I need to fake the navigator.notification.alert method. Can anyone provide suggestions or solutions? Below is the code snippet that I have attempted: navigator = { ...

Adding data from PHP into a designated div element

update: After some trial and error, I managed to find a solution using jQuery and append() methods. From my PHP code, I created an array containing all the necessary information that I wanted to insert into the tab divs. I then encoded this array into JSON ...

Is there a way to disable logging for MongoDatabase connections?

After starting up my Node.js web server and connecting to the MongoDB database, I noticed that sensitive information including my password is being displayed in the console. This could be a security risk as the console may be publicly accessible on some ho ...

Learn how to utilize Vue 3 to access properties that have been passed down from a parent component, even if they

Hey there, hope everything is going well. I'm familiar with react.js, but when I gave vue a try, things felt a bit different. In react, it's easy to access props passed from the parent in the child component without much hassle. However, in vue, ...