# Accessing CZML objects in code

**URL:** <https://community.cesium.com/t/accessing-czml-objects-in-code/3088>\
**Category:** CesiumJS\
**Created:** [September 20, 2015, 4:14pm UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088 "2015-09-20T16:14:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmb10101](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jmb10101](https://community.cesium.com/u/jmb10101)\
**Post date:** [September 20, 2015, 4:14pm UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/1 "2015-09-20T16:14:13Z")

</div>

I am trying to access an Entity defined in a CZML document. I am able to load the CZML and display the trajectory defined within it in the Cesium Viewer. However, when I try to zoom the camera to the object, the object is undefined. Here is the code, the center() function is the key to what I'm trying to do:

&nbsp;&nbsp;var viewer = new Cesium.Viewer("cesiumContainer");  
&nbsp;&nbsp;var data = Cesium.CzmlDataSource.load('SampleData/test.czml') viewer.dataSources.add(data);

&nbsp;&nbsp;&nbsp;function center(){  
&nbsp;&nbsp;&nbsp;&nbsp;var trajectories = data.entities.values;  
&nbsp;&nbsp;&nbsp;&nbsp;var saturnV = trajectories.getById("SaturnV");  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.zoomTo(saturnV);   
&nbsp;&nbsp;&nbsp;}

In the above code, at a breakpoint within center(), data.entities is undefined. How do I access an object by its ID so I can zoom the camera to it? Also, I would like to know how to follow the object along its trajectory, like is shown on the [cesiumjs.org](http://cesiumjs.org) main example.

Thanks

---

<div class="post-metadata">

**Author:** ![Matt\_Amato](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/matt_amato/32/1993_2.png) [@Matt\_Amato](https://community.cesium.com/u/Matt_Amato)\
**Post date:** [September 22, 2015, 12:37am UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/2 "2015-09-22T00:37:18Z")

</div>

The problem is the line:

var trajectories = data.entities.values;

which returns an array. You want to use the actual EntityCollection, which is:

var trajectories = data.entities;

---

<div class="post-metadata">

**Author:** ![jmb10101](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jmb10101](https://community.cesium.com/u/jmb10101)\
**Post date:** [September 22, 2015, 12:43am UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/3 "2015-09-22T00:43:00Z")

</div>

Right, I've fixed that error, and there was another error in the code I posted where I left out a semicolon. But the problem remains since data.entities is undefined when the function is called. data is valid though.

---

<div class="post-metadata">

**Author:** ![Matt\_Amato](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/matt_amato/32/1993_2.png) [@Matt\_Amato](https://community.cesium.com/u/Matt_Amato)\
**Post date:** [September 22, 2015, 12:50am UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/4 "2015-09-22T00:50:44Z")

</div>

Sorry, I didn’t look closely enough. Another problem is this line

var data = Cesium.CzmlDataSource.load(‘SampleData/test.czml’);

data is actually a promise to the loaded data source, not the data source itself, so you need to wait for the promise to resolve before you can use it.

viewer.dataSources.add(data);

data.then(function(dataSource){

//Do stuff with the dataSource

});

---

<div class="post-metadata">

**Author:** ![jmb10101](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jmb10101](https://community.cesium.com/u/jmb10101)\
**Post date:** [September 22, 2015, 3:15pm UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/5 "2015-09-22T15:15:00Z")

</div>

Awesome, thanks.

My next question is how do I get the camera to follow that object along its trajectory?

---

<div class="post-metadata">

**Author:** ![jmb10101](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jmb10101](https://community.cesium.com/u/jmb10101)\
**Post date:** [September 22, 2015, 4:10pm UTC](https://community.cesium.com/t/accessing-czml-objects-in-code/3088/6 "2015-09-22T16:10:04Z")

</div>

solved, thanks
