0

I have a tracker in one of my classes, this class imports a variable from another class that does some computations and returns a array of objects. I am then trying to populate a table with this data. It usually works find when I am pulling data for the db, because the tracker puts a dependency on that that but when I am using an array It does not seem to put a dependency on the variable and only running the tracker once which in turn returns a half empty array.

As you can see in this example I have badDebtsNameSpace is the array variable imported from another class. But it always returns undefined .

Tracker.autorun(function() { // Tracker function for reactivity

        if(typeof badDebtsNameSpace.globalDebCalc != "undefined"){
            cashInData[0] = badDebtsNameSpace.globalDebCalc['fromOpeningDeb'];
            cashInTable.loadData(cashInData);

            cashInTable.render();
        }

        console.log(badDebtsNameSpace)



});

How ever if I look at the printed data from the console log everything seems to be fine. Any ideas on why this is happening.

Thanks

Update

More code as requested. Here is some of the output class that exports the variables.

export var prodNameSpace = {};
export var overheadsNameSpace = {};
export var fixedAssetsName = {};
export var fundingNameSpace = {};


export var globalDebCalc;
export var badDebtsNameSpace;

export var productSummary;
export var assetSummary;
export var vatSummary;
export var overheadsSummary;
export var fundingSummary;

//Overheads Tracker TO Calc Data
    Tracker.autorun(function() { // Tracker function for reactivity
        var tempData = Session.get('data');
        var overs = Overhead.find({fileId: tempData._id}).fetch();
        var globalData = GlobalWhatIfData.find({fileId: tempData._id}).fetch();

         if(overs.length > 0 && globalData.length > 0){
            for(var i in overs){
                var percentageOf = PercentageOfData.find({parentId: overs[i]._id}).fetch();
                var vatRef = VatDataRef.find({parentId: overs[i]._id}).fetch();

                if(percentageOf.length > 0 && vatRef.length > 0){
                    var n = overs[i].description;
                    overheadsNameSpace[n] = new OverheadCalc(productSummary, percentageOf, vatRef, globalData);
                }

            }   
            overheadsSummary = new OverheadsCalc(overheadsNameSpace);
         }
    });

    //Bad Debt Tracker 
    Tracker.autorun(function() { // Tracker function for reactivity
        var tempData = Session.get('data');
        var bds = BadDebts.find({fileId: tempData._id}).fetch();
        var globaDeb = GlobalDebtors.find({fileId: tempData._id}).fetch();

        if(bds.length > 0 && globaDeb.length > 0){
            for(var i in bds){
                badDebtsNameSpace = new BadDebtsCalc(bds, productSummary, globaDeb);
            }
        }
    });
  • 1
    One of the things with trackers is that they require *reactive* data sources in order to be triggered. Is `badDebtsNameSpace.globalDebCalc` a reactive data source? That won't affect whether it's defined or not however, you probably have some other problem with your imports/exports. – Michel Floyd Dec 08 '16 at 21:59
  • The export are calc done from database data . They are also depening on trackers – Amy Teresa Hyland Dec 08 '16 at 22:51
  • What would think if I used reactive vars some how – Amy Teresa Hyland Dec 08 '16 at 22:56
  • I think we need to see more of your code. – Michel Floyd Dec 09 '16 at 01:13
  • `badDebtsNameSpace` doesn't look like it's reactive. Also why are you reassigning it `bds` times? That loop makes no sense. What does the `BadDebtsCalc` function return? – Michel Floyd Dec 10 '16 at 01:15
  • Hey @MichelFloyd I am running the bds time because I want to create a BadDebtsCalc object for each BadDebt return. So just say the user has 3 bad debts it creates a object for each one which does calculations and then that is what i would like to show later. how can I make this reactive. I am probably after going about this all wrong. Thanks for sticking with me here . – Amy Teresa Hyland Dec 10 '16 at 14:40
  • You're actually overwriting the same object over and over. You should create a *reactive array* and then push elements into it. – Michel Floyd Dec 10 '16 at 19:29

1 Answers1

0

You can do this with a reactive array as I mentioned in the comments but if you don't want to add a package you can use a tracker dependency This basically registers a dependency on some other computation so that Tracker can react to it. Also you shouldn't iterate over arrays with for(var i in array) Code below:

export var prodNameSpace = {};
export var overheadsNameSpace = {};
export var fixedAssetsName = {};
export var fundingNameSpace = {};

export var globalDebCalc;
export var badDebtsNameSpace = [];
export var bdnsDep = new Tracker.Dependency; // register a dependency

export var productSummary;
export var assetSummary;
export var vatSummary;
export var overheadsSummary;
export var fundingSummary;

//Overheads Tracker TO Calc Data
Tracker.autorun(function() { // Tracker function for reactivity
  var id = Session.get('data')._id; // small simplification
  var overs = Overhead.find({fileId: id}).fetch();
  var globalData = GlobalWhatIfData.find({fileId: id}).fetch();

  if(globalData.length){ // you don't need to check >0 as 0 will be *falsy*
    overs.forEach(o=>{
      var percentageOf = PercentageOfData.find({parentId: o._id}).fetch();
      var vatRef = VatDataRef.find({parentId: o._id}).fetch();

      if(percentageOf.length && vatRef.length){
        var n = o.description;
        overheadsNameSpace[n] = new OverheadCalc(productSummary, percentageOf, vatRef, globalData);
      }    
    }   
    overheadsSummary = new OverheadsCalc(overheadsNameSpace);
  }
});

//Bad Debt Tracker 
Tracker.autorun(function() { // Tracker function for reactivity
  var id = Session.get('data')._id;
  var bds = BadDebts.find({fileId: id}).fetch();
  var globaDeb = GlobalDebtors.find({fileId: id}).fetch();

  if(bds.length && globaDeb.length){
    bdnsDep.changed(); // tell the tracker to register the change
    for(var i = 0; i < bds.length; i++){
      badDebtsNameSpace[i] = new BadDebtsCalc(bds, productSummary, globaDeb);
    }
  }
});

Tracker.autorun(function() { // Tracker function for reactivity
  bdnsDep.depend(); // this is where we react to the change
  if( badDebtsNameSpace.globalDebCalc ){ // you don't have to check the type, just check to see if the key exists
    cashInData[0] = badDebtsNameSpace.globalDebCalc['fromOpeningDeb'];
    cashInTable.loadData(cashInData);
    cashInTable.render();
  }   
});
Community
  • 1
  • 1
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39