0

I am trying to work with a timer in Salesforce Lightning. When I add it to the page, it throws the error of "render threw an error in 'c:serviceConsoleCaseTimer' [Cannot assign to read only property 'disconnectedHandler' of object '[object Object]']" I have no clue how to fix this error. Here is the code where I am getting this error:

set tabclosed(value){
        this._tabclosed = value;
        this.tabisclosed = this._tabclosed;
        if(this.tabisclosed){
            this.disconnectedHandler();
        }
    }


    constructor(params){
        super(params)

        this.disconnectedHandler = this.disconnectedHandler.bind(this)
        this.pauseTimer = this.pauseTimer.bind(this)
     }

    connectedCallback() {
        if(this.autoStart){              
            this.start();
        }
        window.addEventListener("beforeunload", this.disconnectedHandler); 
    }       

    // Function for detecting window navigation/closing 
    disconnectedHandler(){
        if(this.stime !== '00:00:00'){       
        this.stop();        
        newSession({caseId: this.recordId, timeVal: this.stime}).then(() => {
            })
            .catch(error => {
            });
        }
    }

I understand that the property is read only. I have tried to set the property writable : true as well.

Here are the links that I have tried: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Read-only https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

https://stackoverflow.com/questions/57591012/ngrx-cannot-assign-to-read-only-property-property-of-object-object Workspace Api not returning focused tab details

Thank you for any help!

4
  • Why are you trying to bind the function? It should already be bound to this, effectively meaning that this code would do nothing even if it worked.
    – sfdcfox
    Commented Nov 21, 2019 at 20:42
  • I was trying instantiate them in the constructor so that when window opens, timer starts itself. What do you think that should be done? Commented Nov 21, 2019 at 21:13
  • can you provide playground link? Commented Nov 25, 2019 at 20:35
  • @sfdcfox you were right about the bind. I was able to solve it by removing the bind Commented Nov 26, 2019 at 13:46

1 Answer 1

0
set tabclosed(value){
        this._tabclosed = value;
        this.tabisclosed = this._tabclosed;
        if(this.tabisclosed){
            this.disconnectedHandler();
        }
    }


    constructor(params){
        super(params)

        this.disconnectedHandler()
        this.pauseTimer()
    }

    connectedCallback() {
        if(this.autoStart){              
            this.start();
        }
        window.addEventListener("beforeunload", this.disconnectedHandler); 
    }       

    // Function for detecting window navigation/closing 
    disconnectedHandler(){
        if(this.stime !== '00:00:00'){       
        this.stop();        
        newSession({caseId: this.recordId, timeVal: this.stime}).then(() => {
            })
            .catch(error => {
            });
        }
    }

You must log in to answer this question.

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