I want to overwrite entity values in CzmlDataSource

Hello All,

I am attempting to overwrite the values of an entity in a CzmlDataSource and having trouble.

I have the following code snippet and when I console.log the myEntities I get back the array I am looking for, however I can not seem to access the array and I am not sure why. Anything I try returns undefined, so there is something I am not understanding about Entity or Entity Collections in Cesium. If I log myEntities.length that returns 0 and I am not sure why. I feel like I am missing something really simple here.

const dsOne = new Cesium.CzmlDataSource();

const dsOneUrl = ‘user input: datasource location URL’;

dsOne.load(dsOneUrl);

this.viewer.dataSources.add(dsOne);

const myEntities = dsOne.entities.values;

//this retruns the array I am looking for with all 6 values, however I can not seeem to access it

console.log(myEntities)

//this returns 0

console.log(myEntities.length);

The following is the screenshot of the console for myEntities

log.png

Ultimately I am looking to dynamically change the billboard image for my entities which are initially set int the Czml file.

Thanks in advance,

Chase

Using Cesium 1.50, with Chrome on Windows 10

The load method is asynchronous, which you can see in the documentation that it returns a Promise.

https://cesiumjs.org/Cesium/Build/Documentation/CzmlDataSource.html#.load

So you need to wait for load to finish by using .then()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

log.png

Thank you Scott, and I have seen where you have addressed this previously in other posts (I have been looking) but did not fully understand, so thank you for taking the time to assist.
I have updated the code to the following and expected to see the console.log output, but I do not. If I have misunderstood the documentation references could you please let me know how to resolve this.

const dsOne = new Cesium.CzmlDataSource();

const dsOneUrl = ‘user input: datasource location URL’;

dsOne.load(dsOneUrl).then(function (dataSource) {

this.viewer.dataSources.add(dataSource);

const entities = dataSource.entities.values;

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

   const entity = entities[i];

   console.log(entity);

}

});

Regards,

Chase

Most likely your error is in accessing this.viewer which is probably undefined from within the callback, which is then throwing an error and halting execution. Rather than relying on console.log for debugging, I would recommend getting familiar with the browser’s JavaScript development tools to set breakpoints and step through your code line-by-line.

Scott, Thank you, that was it! I have the objects now.
Will dig into the dev tools as suggested as well.