Accessing CZML objects in code

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:

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

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

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 main example.

Thanks

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;

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.

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

});

Awesome, thanks.

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

solved, thanks