4

I am trying to call an Apex method imperatively from LWC. The method accepts an Integer parameter. When I call this Apex method using a wired method it works fine, without any error. But when I call it imperatively from the LWC, it throws an error Value provided is invalid for action parameter 'i' of type 'Integer'

import { LightningElement, track, wire } from 'lwc';
import method from '@salesforce/apex/MyClass.method';

export default class MyComponent extends LightningElement {
  i = 5;
  @track messageFromImperative = '';
  @track messageFromWiredCall = '';
  @track errorFromImperative = '';
  @track errorFromWiredCall = '';

  @wire(method, {i: '$i'}) 
  getMessage({data, error}) {
    this.messageFromWiredCall = data;
    this.errorFromWiredCall = error;
  }

  handleClick() {
    method({i: '$i'})
      .then(result => { this.messageFromImperative = result })
      .catch(error => { this.errorFromImperative = error })
  }
}
//Apex class
public with sharing class MyClass {
    @AuraEnabled(cacheable=true)
    public static string method(Integer i){
        return 'Integer sent in '+ i;
    }
}

1 Answer 1

8

In case of imperative call, the parameter you are sending to apex shouldn't be reactive, so instead of writing
{i: '$i'}

you need to write<

{i: this.i}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .