Need assistance with resolving this issue: Uncaught (in promise) TypeError: Unable to access properties of undefined (reading 'length')

I am currently working on a project involving the loading of gltf 3D models on a website. I am looking to dynamically load multiple models using a loop.

const loader = new GLTFLoader()
//.setPath( 'models/gltf/DamagedHelmet/glTF/' );
      .setPath( 'resources/' );
const resourceData = ["Learning Bee1","Learning Bee2","Learning Bee3"];
//const l = resourceData.length;

for(let i=0; i<resourceData.length;i++){
    let oResource = resourceData[i];
    let sModelName = oResource + ".gltf";
    loader.load( sModelName, function ( gltf ) {

        gltf.scene.traverse( function ( child ) {

            if ( child.isMesh ) {

                roughnessMipmapper.generateMipmaps( child.material );

            }

        });
    });

    scene.add( gltf.scene );

    roughnessMipmapper.dispose();

    render();

}

});

After running this code, I encountered the following error message: "three.module.js:38723 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length'). How can I resolve this issue?

Answer №1

Your lack of proper code indentation is leading to mistakes that could easily be avoided with cleaner code.

  1. It seems like you are calling gltf.scene outside of the brackets where it is defined:
loader.load( sModelName, function ( gltf ) {
    // gltf is accessible here
});

// gltf is not accessible here
scene.add( gltf.scene );
  1. You are disposing of the roughnessMipmapper before actually using it. Remember that the callback function in the loader may take some time to execute as it waits for assets to finish loading.
loader.load( sModelName, function ( gltf ) {
    // 2. This step occurs after assets have finished loading
    roughnessMipmapper.generateMipmaps( child.material );
});

// 1. This step is executed first
roughnessMipmapper.dispose();

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

Applying Media Queries Based on Element Height

Situation: In my scenario, there is an image with two buttons below it, all of which share the same width. Issue: When the viewport is too wide horizontally, the buttons are not visible without scrolling down. This poses a challenge for tablets and small ...

utilizing an ajax request to clear the contents of the div

When I click on Link1 Button, I want to use ajax to empty the contents in the products_list div <button type="w3-button">Link1</button> I need help with creating an ajax call that will clear the products in the product_list when the link1 but ...

Transfer the index of a for loop to another function

Can you explain how to pass the 'i' value of a for loop to a different function? I want to create a click function that changes the left position of a <ul> element. Each click should use values stored in an array based on their index posi ...

Showing user data in a div using JavaScript ajax without the use of jQuery

Recently diving into the world of javascript and ajax, I find myself tinkering with code on jsfiddle. My current goal is to populate a list with usernames and emails upon form submission. Upon submitting the form, an error message pops up: {"error":"key m ...

Tips for clearing a textbox value in JQuery after a 5-second delay

I attempted the following: <script type="text/javascript"> $(function() { $("#button").click( function() { alert('button clicked'); // this is executed setTimeout(function() ...

What is the proper way to parse an array of JSON objects?

Here is the data I need help with: var information = [ { "_id": "5e458e2ccf9b1326f11b5079", "xxTitle": "testtttttttttttt", "hhhhhhhhhh": "sssssssss", "xxzzzzzz": null, "oooooooooooooo": "ssssss", "xxDescription": "sssssss", "xxDetails": "ssssssss", "llll. ...

Using PHP to insert additional elements into an array during a loop iterator

I would appreciate if someone could help me identify the mistake in my code, which appears as follows: $arrayOfDays = array(); for($dayIterator=$from; $dayIterator < $to; $dayIterator->modify('+1 day')){ $arrayOfDays[] = $dayIt ...

After completing the payment, Paypal Express checkout causes the page to refresh

I have integrated an Express checkout button into my webpage and configured the server-side process with client code: <script src="https://www.paypalobjects.com/api/checkout.js"></script> <script> var CREATE_PAYMENT_URL = "<create ...

Issues with Custom Post Type Taxonomies - Categories and Tags not displaying associated posts

I recently created a custom post type called "Member Resources" with specific taxonomies like categories and tags. For example, I have tags such as "Diversity" and categories like "Guidance". However, when trying to access the posts under these taxonomie ...

I understand the reason behind the unexpected { token error, but I'm unsure of how to resolve it when my PHP script needs to transmit a collection of data to JavaScript

I am currently utilizing this JavaScript fetch code to retrieve data from PHP async sendRequest(selectValue=this.selectValue){ const fetchResponse = await fetch('/server/getLastWords.php?select='+selectValue); const fetchJSON = await fe ...

Is it possible for one AngularJS application to influence another?

In my latest AngularJS project, I developed an application for user creation. Depending on the selected user type, specific input fields are displayed. Upon submission, the data is sent to the server in JSON format. So far, everything is working smoothly. ...

Utilizing a method to nest schemas within schemas thanks to Mongoose

I am currently working on developing a blog that utilizes Node.js as the server-side language and integrates with Mongo DB. Within my blog, users will have the ability to create posts and interact through commenting, just like any typical blog platform. ...

When attempting to pre-render a Next.js page using getStaticProps(), an error occurs stating that the image is missing the required "src" attribute when using next/image

After reading the documentation for nextjs, I learned that the getStaticProps function should pre-render data that I need before a user visits my site. My goal is to fetch images and load them onto cards. In my index.js file: export async function getSta ...

Steps for making a webpack-bundled function accessible globally

I am currently working with a webpack-bundled TypeScript file that contains a function I need to access from the global scope. Here is an example of the code: // bundled.ts import * as Excel from 'exceljs'; import { saveAs } from 'file-save ...

Learn how to import from a .storybook.ts file in Vue with TypeScript and Storybook, including how to manage Webpack configurations

I'm currently utilizing Vue with TypeScript in Storybook. Unfortunately, there are no official TypeScript configurations available for using Vue with Storybook. How can I set up Webpack so that I am able to import from another .storybook.ts file with ...

Having issues with the `onlyCountries` property in the React phone input 2 component when using

I've integrated the react-phone-input-2 library to include phone number fields in my project. I need to provide selected countries only for the country codes. I'm fetching iso2countrycodes from the server and passing them to my Phone input compon ...

Center your attention on an AngularJS-created input element

I'm currently working on a todo list project using AngularJS and I am wondering if there is a method to automatically focus on an input box after creating it by clicking on a button. As of now, the save function in my controller looks like this: $sc ...

What is the process for adjusting the width of an element using JavaScript?

I have a unique bar with one half red and the other green. I am trying to subtract 1vw from the width of the red section. Unfortunately, using style.width is not yielding the desired result. See below for the code snippet I am currently using: //FIGHT do ...

Picking an element that has a dynamic data attribute, but also considering those with a hyphen

Currently, I am developing a function that will select a span element when clicked based on its data attribute and value. Here is the code snippet: function moveFilterElements(event) { if ($(event).hasClass('active')) { var dataAtt ...

What could possibly be causing the notification to fail to function in my deferred scenario?

I'm currently delving into the world of jquery deferred and making good progress, but I have encountered a hurdle when it comes to the notify feature. In my code snippet, you will notice that I attempted to use the notify method, only to find out that ...