+ Start a Discussion
Christos KolonisChristos Kolonis 

Pass JavaScript Variable To Apex Controller in LWC


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(getInvoices,{customerId: '$recordId', fromDate: '$fromDT', toDate: '$toDT',meterNum: '$selectedmeter',subscriptionNUm: '$selectedname', PagePosition: '$pagePos'})

My Apex Controller:

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.


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:');
        // handle your error.

Let me know if this helps