In node.js, how to use node.js and mongodb to store data in multiple levels

I met a wired problem, that when i use mongodb to store data, some data is missing, which I think it is because of its asynchronous feature

So for this list the timetable, I would use re

/* Here is the a application, in which by using a train_uid and today, */

var today = new Date();
var day = today.getDay();
scheduleModel.findByTrainAndTime(train_uid,today,function(err, doc){
    var a = new Object();
    if(err){}
    else{
        if(doc != null)
        {   

//mongodb database can give me some data about the train_id ,uid

        a.train_uid = doc.train_uid;
        a.train_id = train_id;

and most importantly a train schedule time table, the train schedule time table is a list ( doc.time_schedule )of json objects like arrival, departure and tiploc. However, I need to change the tiploc to sanox number, which referenceModel can help find sanox by providing tiploc number.

                   //doc.time_schedule
                   // here is to add a array

so I use async, for each item in the list, I use referenceModel to query sanox and construct a array - a.timeline to store each b, at last when async each operation is finished, trainModel is to store a object with an array of sanox object. However when it comes to the mongodb database, only the array of sanox objects are empty, I guess it is because of asynchronous operation, but since I used async , why it doesn't work

                    a.train_uid = doc.train_uid;  //works
        a.train_id = train_id;   works
                    a.timeline = []   // doesn't work    
        a.timeline = new Array();                                               
        var b ;
            async.forEachSeries(doc.time_schedule,
              function(item,callback){
                   referenceModel.findStanoxByTicloc(item.tiploc_code,function(err,sanox){ 
                      try{ 
                        b = new Object();
                                            b.sanox = sanox;
                        a.time.push(b);

                      }catch(err2){

                      }
                  });   
                  callback();
              },
              function(err){
                trainModel.createNewTrain(a,function(){}); 

        }
    }
 });

You're calling callback after you fire off the asynchronous find, but before it actually comes back. You need to wait until after you've gotten the data to do that. The following should work better:

    async.forEachSeries(doc.time_schedule,
          function(item,callback){
             referenceModel.findStanoxByTicloc(item.tiploc_code,function(err,sanox){ 
                  try{ 
                    b = new Object();
                    b.sanox = sanox;
                    a.time.push(b);

                  }catch(err2){

                  }
                  callback();
              });   
          },
          function(err){
            trainModel.createNewTrain(a,function(){}); 

    }