2

So, I am doing this:

this.pendingAction = promiseCache(getPromotions, {sellerId : this.item.seller_id, params : null} )

promiseCache is what is sounds like. I pass it the imported apex method I want to call imperatively, and the parameters. 'SellerId' is a String, as defined in the method:

@AuraEnabled
public static String getPromotions(String sellerId, Map<String, Object> params){
    System.debug('lwcPromotionsController sellerId: ' + sellerId);
    String token = AppConnectionController.getTokenFromSeller(sellerId);
    MLPromotionManager manager = new MLPromotionManager(token); //pasarle token dsp
    return manager.userPromotionsRaw(sellerId, params); 
}

For some reason, in the debug it shows a null value. I already made a console.log of the value i'm passing (this.item.seller_id) and it looks fine, and checked what data type the field is in salesforce and it is a text field. What is happening?

EDIT: here is the promiseCache method, in case it has something to do with it:

async function promiseCache(promise, params) {
    params = params || {};
    promiseCache.cache = promiseCache.cache || new Map();
    promiseCache[promise] = promiseCache[promise] || {};

    const hash = JSON.stringify(params);

    if (!promiseCache.cache[promise][hash]) promiseCache.cache[promise][hash] = promise(params);
    return promiseCache.cache[promise][hash];
}
4
  • Wonder if you are getting a circular reference in the params stringify? You have params, with params as a var in params... Mar 3, 2021 at 19:58
  • Also, try the call without your promise cache... you can specify the AuraEnabled method to be cacheable too you know Mar 3, 2021 at 19:59
  • making the auraenabled cacheable does the same thing as the promiseCache method?? (I didn't write that code, that's why I'm asking) Mar 3, 2021 at 20:09
  • Yep. Sure does. Mar 3, 2021 at 20:46

1 Answer 1

2

Your promiseCache function isn't waiting for the results, so it returns null. You need to wait (using await):

async function promiseCache(promise, params) {
    params = params || {};
    promiseCache.cache = promiseCache.cache || new Map();
    promiseCache[promise] = promiseCache[promise] || {};

    const hash = JSON.stringify(params);

    if (!promiseCache.cache[promise][hash]) {
      promiseCache.cache[promise][hash] = await promise(params);
    }
    return promiseCache.cache[promise][hash];
}

In addition, you need to do the same for your method call:

this.pendingAction = await this.promiseCache(getPromotions, {sellerId : this.item.seller_id, params : null} )

The method this is contained in also needs to be async.

P.S. All this code is mostly useless, as @AuraEnabled(cacheable=true) automatically does what you're trying to do here, with an appropriate stale time so you can get fresh values later. I'd recommend ditching this and just using the promise directly.

this.pendingAction = await getPromotions(
  {sellerId : this.item.seller_id, params : null }
);

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.