+ Start a Discussion
Robin BarnwellRobin Barnwell 

LWC publish question

I've got the following lines of code that work just fine when boatId has an Id value (string)
sendMessageService(boatId) { 
    // explicitly pass boatId to the parameter recordId

    const payload = { recordId: boatId };
    publish(this.messageContext, BOATMC, payload);

  }
But when I set boatId to nothing the publish fires but the subscriber never gets the message.  What am I doing wrong, how do I publish a null boatId?
 
Robin BarnwellRobin Barnwell
Solved it!!!  The recordId wants 18 char, without it you don't get the error event in the @wire subscriber. So when you publish just include 18 characters of garbage
 
sendMessageService(boatId) { 
    // explicitly pass boatId to the parameter recordId

    if (boatId.length <1){boatId="123456789123456789";}
    const payload = { recordId: boatId };
    publish(this.messageContext, BOATMC, payload);

  }
This forces the @wire to raise an error rather than silently fail.