scene.add(object) creates an “Uncaught TypeError: Cannot read property 'length' of undefined”

StackOverflow https://stackoverflow.com/questions/8381584

  •  28-10-2019
  •  | 
  •  

Question

Getting that error in my javascript console(in Chrome) with a collada object I'm attempting to add with a basic loader. It's specifically coming from the "scene.add( object )" chunk of it. Everything else seems to work just fine. The code for loading the object is as follows

var ltable;
var furnLoad = new THREE.ColladaLoader();

function addlt(){
        furnLoad.load('../Models/Furniture/foldingLongTable.dae', function(collada){
                ltable = collada.scene;
                ltable.scale.x=ltable.scale.y=ltable.scale.z=1;
                ltable.updateMatrix();
                ltable.position.x=ltable.position.z=ltable.position.y=0;
        });
        scene.add( ltable );
}

This function is called during the init of a page that, otherwise, works just fine. That page can be found here(version without this table has the same URL except for a 4 instead of a 3 at the end), and the specific object here.

What would be the recommended way to get past this error?

Was it helpful?

Solution

RESOLVED. Collada loaders just hate being part of any function and won't work when in them. So the fix is to have them outside of functions and they work just fine.

OTHER TIPS

The answer to this issue is due to the location of the scene.add() being outside of the callback function. It is being called before the callback fires, hence the undefined error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top