0

I am calling lwc method from aura. It gives me error: Cannot read properties of undefined (reading 'then')]. Here is my code. There were some complex logic, but it gives the same error with just returning true;

export default class LwcComponent extends LightningElement {

    @api lwcMethod() {
        return  true; //changed to "return Promise.resolve(true)";
    }

Aura

<aura:component>
    <aura:handler name="init"
                  value="{!this}"
                  action="{!c.onInit}"
    />
    <c:LwcComponent  aura:id="lwcComponent"/>

</aura:component>

({
    onInit: function (component, event, helper) {
        helper.doInit(component);
    }
})

({
    doInit: function (component) {

        component.find('lwcComponent').lwcMethod()
            .then($A.getCallback(function (response) {
                alert(response);
            }));
    },
})

UPD:I debugged and it seems that method even is not called

2
  • It needs to return a Promise for .then to work.
    – Adrian Larson
    Nov 11, 2022 at 14:21
  • @AdrianLarson I tried with promise.resolve and got the same error.
    – Jobaf
    Nov 11, 2022 at 14:42

1 Answer 1

0

Have you tried this? https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.interop_share_javascript

Also here are some communications patterns: https://developer.salesforce.com/blogs/2021/05/inter-component-communication-patterns-for-lightning-web-components

I know these references are saying about data communication, but if we think a little, on javascript a function that could be passed through it,

Question:

  1. Does your component.find('lwcComponent') is returning the element from the DOM?
  2. Which event is not being called? the doInit?
1
  • 1. Yes. 2.lwcMethod. I added console.log and tin debud there are no entry at that point. But function is correct, because I tried with wrong one and there are another error. I will look into first link, thanks.
    – Jobaf
    Nov 11, 2022 at 17:40

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.