2

I need to run this @wire on a click event. I thought on creating, inside the LWC class, a method to call it, but it didn't work.

how can I call the WIRE service inside a function with parameter?

something similar to this:

    myMethod(PARAMETER){
    @wire(getRelatedMedia, { productId: PARAMETER })
    relatedMedia({ error, data }) {
        if (data) {
            // this.contacts = data;
            // this.error = undefined;
            console.log(data);
        } else if (error) {
            console.log('erro');
            // this.error = error;
            // this.contacts = undefined;
        }
    }
    }
8

You can't call a wire from within a function. If you wanted to do this, you'd just set the wire parameter.

myMethod(PARAMETER){
  this.productId = PARAMETER;

...

@wire(getRelatedMedia( { productId: '$productId' })
  relatedMedia({data,error}) {
  // code
}

Instead, you can use an imparative Apex call:

myMethod(PARAMETER){
  getRelatedMedia({productId:PARAMETER})
    .then(data => {
      this.contacts = data;
      this.error = null;
    })
    .catch(error => {
      this.contacts = null;
      this.error = error;
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.