1

I have an error I don't understand when I activate the Debug Mode in my sandbox. Message: [Invariant Violation: Updating the template of [object:vm v (127)] has side effects on the state of [object:vm v (127)].startDate]

@track startDate;
    @track endDate;
    @track month;
    @track diffMonth;
    @track currentMonth;

//Getter
get createHeader(){
    
    this.sDate = new Date(this.opportunity.data.fields.Start_Date__c.value);
    this.startDate = this.sDate.getMonth();
    
    this.currentMonth = this.startDate;

    this.eDate = new Date(this.opportunity.data.fields.End_Date__c.value);
    this.endDate = this.eDate.getMonth();
    
    var month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    
    this.headerArray.splice(0, this.headerArray.length);
    this.amountArray.splice(0, this.amountArray.length);
    this.scheduleMonths.splice(0, this.scheduleMonths.length);


    this.headerArray.push('Product');
    this.headerArray.push('Description');

    this.diffMonth = this.endDate-this.startDate;
    
    if(this.diffMonth < 0){
        this.diffMonth = (this.endDate + 12) - this.startDate;
        var count = this.diffMonth;
        while(count >= 0){
            if(this.currentMonth > 11){
                this.currentMonth = 0;
            }
            this.headerArray.push(month[this.currentMonth]);
            this.amountArray.push('0');
            this.scheduleMonths.push('Schedule ' + month[this.currentMonth]);
            this.currentMonth++;
            count--;
        }
    }else{
        while(this.startDate < (this.endDate + 1)){
            this.headerArray.push(month[this.startDate]);
            this.scheduleMonths.push('Schedule ' + month[this.startDate]);
            this.amountArray.push('0');
            this.startDate++;
        }
    }
    
    return this.headerArray;
}

Thanks Eric

7
  • From what I see it's here somewhere... this.sDate = new Date(this.opportunity.data.fields.Start_Date__c.value); this.startDate = this.sDate.getMonth(); this.currentMonth = this.startDate; this.eDate = new Date(this.opportunity.data.fields.End_Date__c.value); this.endDate = this.eDate.getMonth();
    – Eric
    Mar 25, 2021 at 18:59
  • More precisely, the error comes from this this.startDate = this.sDate.getMonth(); this.endDate = this.eDate.getMonth(); As soon I remove it from comment I have the error
    – Eric
    Mar 25, 2021 at 19:10
  • I edited the code to help
    – Eric
    Mar 25, 2021 at 20:27
  • 1
    I see you have this.sDate... is that a var on the controller? If not, it's undeclared. If you are using as a temp var, just define it as let sDate = ... Mar 25, 2021 at 20:46
  • Yes, tried with var without success and Let works like a charm. Thanks!
    – Eric
    Mar 25, 2021 at 20:54

1 Answer 1

1

From the error message, I think it's because you have not declared the sDate variable in the controller.

It also looks like you probably don't need it defined on the controller; it would be better to just define it using let within the block level scope.

Like:

let sDate = new Date(this.opportunity.data.fields.Start_Date__c.value);
this.startDate = sDate.getMonth();

this.currentMonth = this.startDate;

let eDate = new Date(this.opportunity.data.fields.End_Date__c.value);
this.endDate = eDate.getMonth();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .