0

in my component B after saving the data to the database I am creating a publish call as shown:

handleSubmit(){
        var dataList = {};
        dataList.Id = this.data[0].Id;
        dataList.Name = this.name;
        dataList.DYNMPR__Active__c = this.active;
        dataList.DYNMPR__SourceObject__c = this.sourceObj;
        dataList.DYNMPR__TargetObject__c = this.targetObj;
        dataList.DYNMPR__Description__c = this.description;;
        this.newData = dataList;
        this.newData = dataList;
        console.log("this.EditData:: " + JSON.stringify(this.newData));
        updateData({mapObjData : this.newData});
        const evt = new ShowToastEvent({
            title: 'Record Update',
            message: 'The Record is Updated ',
            variant: 'success',
            mode: 'dismissable'
        });
        this.dispatchEvent(evt);
        const payload = {
            source: "Lightnign Web Component",
            messageBody: this.newData
        }; 
        publish(this.context, lmsDemoMC, payload);
        console.log('payload::' + JSON.stringify(payload));
        this.closeModal();
      //  return refreshApex(this.newData);

    }

In my component A I am calling the subscribe to get the data as shown:

getMapData(){
        var message;
        var name;
        fetchdata()
            .then(result => {
                console.log('result:::' + result);
                this.data = result;
                this.error = null;
                console.log('Data Connected callback' + JSON.stringify(this.data));
                this.recordid = String(this.data[0].Id);
                this.name = this.data[0].Name;
                this.sourceObj = this.data[0].DYNMPR__SourceObject__c;
                this.targetObj = this.data[0].DYNMPR__TargetObject__c;
                this.description = this.data[0].DYNMPR__Description__c;
                this.active = this.data[0].DYNMPR__Active__c;

                const parentPage = this;
                const name = this;
                this.channel = subscribe(this.context, lmsDemoMC, function (event){
                    console.log('I am here::' + event);
                    if (event != null) {
                        message = event.messageBody;
                        this.data = message;
                        const source = event.source;
                        //parentPage.receivedMessage = 'Message: ' + message + '. Sent From: ' + source;
                    }
                }, {scope: APPLICATION_SCOPE});


            })
            .catch(error =>{
                this.error = error;
                this.errorMsg = error;
                this.isEmpty = true;
            });  
    }

I am getting the message in, message = event.messageBody; and I want to save this message to this.data (declared as @track data) so that the latest data is shown on the UI. But its not working.

message that I have received from subscription :

  message::{"Id":"a013i000007L9qUAAS","Name":"Discharge Case Follow Up Mapping1","DYNMPR__Active__c":true,"DYNMPR__SourceObject__c":"Case","DYNMPR__TargetObject__c":"Case"}

when I print this.data nothing comes up on the console.

6
  • Do you see any errors in the browser's console? Are any errors presented directly on the page? Have you tried debugging using the browser's javascript debugger? Please edit the question to add details about what you see and what you have tried to do to investigate the problem.
    – Phil W
    Commented May 13, 2020 at 8:27
  • @PhilW I have added the result from the console. I get the message but it is not reaching the line this.data=message. Commented May 13, 2020 at 9:07
  • did you try parentPage.data=message since you have taken parentPage = this;
    – User6670
    Commented May 13, 2020 at 9:14
  • @User6670 Yes I did try that and I am getting the message on it as well, but not on this.data since I am using this.data to update the values on the UI. Commented May 13, 2020 at 9:32
  • so inside the function you can assign value to data using "parentPage.data" but it will be referencing the same "data" variable that you have tracked.
    – User6670
    Commented May 13, 2020 at 9:38

1 Answer 1

0

You should Use "parentPage" to get access to data variable since we have assigned "this" to "parentPage"

 if (event != null) {
      message = event.messageBody;
      const source = event.source;
      parentPage.data=message;

 }

You must log in to answer this question.

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