+ Start a Discussion
Christos KolonisChristos Kolonis 

Pass JavaScript Variable To Apex Controller in LWC

Hello,

I am trying to pass a value of a variable to my apex class controller but i get that error:

el_getCustomerInvoices.js:4 Uncaught (in promise) ReferenceError: LWC component's @wire target property or method threw an error during value provisioning. Original error:
[pagePos is not defined]

My js code is this:
 
@api recordId;
@api subscriptionfield=false;
@api fromDT;
@api toDT;
@api pagePos = 0;
...... 

WIRE:

 @wire(getInvoices,{customerId: '$recordId', fromDate: '$fromDT', toDate: '$toDT',meterNum: '$selectedmeter',subscriptionNUm: '$selectedname', PagePosition: '$pagePos'})

My Apex Controller:

@auraEnabled(cacheable=true)
public static List<el_invoices> getInvoices(id customerId, String fromDate, String toDate, String meterNum, String subscriptionNUm, Integer PagePosition){
....

i need to pass the pagePos to my apex. Any ideas?

Thank you!
VinayVinay (Salesforce Developers) 
Hi Christos,

You can try using apex:param.

References:
https://salesforce.stackexchange.com/questions/276407/pass-javascript-variable-to-apex-controller-in-lwc/276451
https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller
https://developer.salesforce.com/blogs/developer-relations/2009/10/passing-javascript-values-to-apex-controller.html

Thanks,
AnudeepAnudeep (Salesforce Developers) 
A similar question is answered here

You are likely getting this error because an error must have occurred during your wire process.

From the documentation:

data (Any type)—The value supplied by the adapter.

error (Error)—An error if the adapter wasn’t able to supply the requested data or if the adapter wasn’t found. Otherwise, this property is undefined.

What you need to do is add an if statement to verify if data came back or not like so:

@wire(getListUi, {objectApiName: PRODUCT_OBJECT})
wiredlistView({error,data}) {
    if (data) {
        this.allListViews = data.lists;
        var listViewData = [];
        for(var i=0;i<this.allListViews.length;i++){
            listViewData.push({"label" : this.allListViews[i].label, "value" : this.allListViews[i].apiName});
    } else if (error) {
        console.log('An error has occurred:');
        console.log(error);
        // handle your error.
    }
}

Let me know if this helps