2

While working with @wire, I figured out that the data returned from the apex method is not available to use on component load. However, after load, when I I called a method(triggered through event), the data from apex method became accessable.

 @wire (getCurrencySymbol)
    objCurrency({data,error}){
        if(data)
            this.sCurrency = data;
        else
            this.sCurrencyError = error;
    }
get currencySymbol(){
        // eslint-disable-next-line no-console
        console.log('this.sCurrency get : ',this.sCurrency);
        return this.sCurrency;
    }

The data is not accessible inside js using any of the lifecycle hooks.

What I want here is to use this.sCurrency which should hold returned data inside another method which is called from connectedcallback and do some processing. Also, if I use {currencySymbol} on html, it displays the value returned from apex method but is still not accessible inside the other method(which I haven't added in the below code snipper).

Any idea how this can be accomplished?

2
  • have you tried declaring it as a property? also Use Getters and Setters to Modify Data – glls Sep 6 '19 at 1:26
  • Wire methods are called asynchronously, and are therefore never available in connectedCallback, even if the result is currently cached. Instead, you'll need to perform any work you need to do in either a setter, or in the callback for the wired method. The bit you've omitted is probably more important than you realize. – sfdcfox Sep 6 '19 at 3:58
4

Wire service is async in nature and all wire services returns promise - which nobody can tell when the response is returned. Ofcourse initially wire service is triggered while registering it when it gets both data and error as undefined but it will fire again when all dependent parameters are valid - in your case there are no parameters, hence it will be triggered again after the process of registration.

Wire service is something comparable to connectedCallback - you cannot invoke it manually though javascript, it will be self invoked only when dependent parameters are valid and changed. So, you need to invoke dependent methods from within wired function:

@track objcurr;
@wire(getCurrencySymbol)
objCurrency({ data, error }) {
    if (data) {
        this.objcurr = data; // not needed if objcurr not used in HTML
        this.dependentMethod(this.objcurr);
    } else if (error) {
        console.error("ERROR ", error);
    }
}

dependentMethod(currData) {
    console.log('currData => ', JSON.stringify(currData));
    // console.log('this.objcurr => ', JSON.stringify(this.objcurr));
}
1
  • Is this issue still not solved? @AshutoshArora – salesforce-sas Sep 19 '19 at 17:13

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.