0

I have an Aura method which returns Boolean

public static Boolean addasFav(Id pProductId) {...}

I want to call this in my lwc js

I have added the below code

import addasFav from '@salesforce/apex/ProductController.addasFav';

export default class ProductTile extends LightningElement {

////Some Code      

var productId = this.product.Id; //Returning vaue 

@wire(addasFav,{pProductId:'$productId'})
        value;
}

How do I retrieve the Boolean value. The Wire syntax is giving me error

  1. Declaration expected
  2. "message": "LWC1007: d:\VSCode\HelloWorldLightningWebComponent\t1\productTile.js: Leading decorators must be attached to a class declaration (55:4)",
2
  • 1
    The "productId" must be a property of the LWC itself, not declared as a var. You probably want to annotate it as @track to make it reactive in the call to the wire. – Phil W Nov 25 '20 at 12:23
  • @track is no longer required, but yes, productId should be a property – willard Nov 25 '20 at 15:37
0

The productId must be defined as the property in the LWC component. You can define that in two ways.

A Define the attribute.

import addasFav from '@salesforce/apex/ProductController.addasFav';
export default class ProductTile extends LightningElement {
    productId;

    // asign some value later.
    foo(){
     ...
     this.productId = somevalue;
    }

    //...
}

B Define getter. Specifically for your example, I think getter will be appropriate. Whenever the product is updated, productId also updated.

get productId(){
    return this.product ? this.product.Id ? undefined;
}

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.