Datasource entities undefined when I try to do a getById after opening a czml datasource.

Hello,

I can’t figure out why I cannot access the entities of a czml datasource after it has been c

This code works OK:

var czmlData = Cesium.CzmlDataSource.load(‘…/sampledata/Otto.czml’);

czmlData.then(function(x) {

viewer.dataSources.add(x); => Entity is visible OK

viewer.zoomTo(x); => Entity is zoomed to OK

viewer.trackedEntity = x.entities.getById(‘Otto’); => Entity is tracked OK (this leads me to believe that the datasource is working OK)

});

This code fails to access entity with getById()

var czmlData = new Cesium.CzmlDataSource();

czmlData.load(‘…/sampledata/Otto.czml’);

*viewer.dataSources.add(czmlData); *=> Entity is visible OK

*viewer.zoomTo(czmlData); *=> Entity is zoomed to OK

var id = czmlData.entities.getById(‘Otto’);

console.log(id); => Result in console:‘Undefined’. But no error message in the console

This code fails also but does throw a error message in the console:

var czmlData = new Cesium.CzmlDataSource.load*(‘…/sampledata/Otto.czml’); *!! Notice the load being called directly

*viewer.dataSources.add(czmlData); *=> Entity is visible OK

*viewer.zoomTo(czmlData); *=> Entity is zoomed to OK but screen remain stuck on LOADING

var id = czmlData.entities.getById(‘Otto’);

*console.log(id); => Result: *Uncaught TypeError: Cannot read property ‘getById’ of undefined (on line 70)

So, aside from the small point I don’t understand why in case 2 there is no error message and in case 3 there is; my major question is, why is czmlData undefined in cases 2 and 3 while, based on the showcase CZML custom properties, it should work (https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/?src=CZML%20Custom%20Properties.html&label=All)

Many thanks in advance,

Filip S.

Case 1 is giving you the answer. CzmlDataSource.load() returns a promise that you are handling properly.
Cases 2 and 3 work partially because of the way the other calls deal with promises. Usually by waiting for the promise to be resolved as you did in case 1.

Hope that helps.

Scott

Hi Filip,

Exactly as Scott says, CzmlDataSource.load() returns a Promise that resolves to the dataSource. Take a look at this article on Promises, we recommend our developers all read it.

Thanks,
Gabby

Thanx for the replies. The article helped me better understand.