0

Hi I am trying to using Lightning Message Service my Component structure is as below

Parent Component Component1 Component2

from Component1 I am publishing a payload via LMS which contains

name date1 date2

in Component2 I am subscribing to same LMS but receiving an error (in promise) Error: lightning/messageService: invalid message context

Here is my code

Lightning Message Service

<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <masterLabel>searchCM</masterLabel>
    <isExposed>true</isExposed>
    <description>Message Channel to pass Count updates</description>
    <lightningMessageFields>
        <fieldName>Name</fieldName>
        <description>null</description>
    </lightningMessageFields>
    <lightningMessageFields>
        <fieldName>searchafterDatVal</fieldName>
        <description>null</description>
    </lightningMessageFields>
    <lightningMessageFields>
        <fieldName>searchbeforeDatVal</fieldName>
        <description>null</description>
    </lightningMessageFields>
</LightningMessageChannel>

Component1.js

import { LightningElement ,wire} from 'lwc';
import { publish, MessageContext} from "lightning/messageService";
import searchCmValue from "@salesforce/messageChannel/searchCM__c";
export default class RsLwcSearchBox extends LightningElement {
    @wire(MessageContext)
    messageContext;   
    searchValue = '';
    searchafterDatVal = null;
    searchbeforeDatVal = null;

    handleSearchKeyword(event){ //this method is getting called on a button click

        var inp = this.template.querySelectorAll("lightning-input");

        inp.forEach(function (element) {
            if (element.name == 'name') {
                this.searchValue = element.value;
                if(this.searchValue == ''){
                    this.searchValue = null;
                }
            }

            if (element.name == 'afterDateValue') {
                this.searchafterDatVal = element.value;
                if(this.searchafterDatVal == ''){
                    this.searchafterDatVal = null;
                }
            }
            if (element.name == 'beforeDateValue') {
                this.searchbeforeDatVal = element.value;
                if(this.searchbeforeDatVal == ''){
                    this.searchbeforeDatVal = null;
                }
            }
        }, this);

        const payload = { 
            Name: this.searchValue,
            searchafterDatVal: this.searchafterDatVal,
            searchbeforeDatVal : this.searchbeforeDatVal
          };
          **publish(this.messageContext, searchCmValue, payload);**
    }
}

Component2.js

import { LightningElement, wire, api, track } from 'lwc';
import { publish, subscribe, MessageContext } from "lightning/messageService";
import messageOfCampId from "@salesforce/messageChannel/campIdMessage__c";
import searchCmValue from "@salesforce/messageChannel/searchCM__c";

export default class RsLwcAppPage extends NavigationMixin(LightningElement){

    subscription = null;

    @wire(MessageContext)
    messageContext;

    connectedCallback() {
        this.subscribeToMessageChannel();
    }
    subscribeToMessageChannel() {
        if (!this.subscription) {
            console.log('in subscription')
            this.subscription = subscribe(
                this.messageContext,
                searchCmValue,
                (message) => this.handleSearchKeyword(message)
            );
            console.log('this.subscription ', this.subscription);
        }
    }

    handleSearchKeyword(message) {
        console.log('messageeee ', message);
    }
}

0

You must log in to answer this question.