function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Al BeAl Be 

Lightning Component: doInit is not defined

Hello!
I have a Lightning Component PortalHeader
PortalHeader.cmp looks like this:
<aura:component controller="currentUserInfoCtrl" description="portalHeader" implements="forceCommunity:availableForAllPageTypes" access="global">
    <aura:attribute name="userInfo" type="user"></aura:attribute>
    <aura:handler name="init" value="{!this}" action="{!c.initMe}"/>
    
    <div id="ui-wrapper">
        <div class="container">
    ...
PortalHeaderController.js:
({
    initMe : function(component, event, helper) {

        var action = component.get("c.fetchUser");
    ...
and I am getting an error: 
> ***doInit** is not defined throws at https://.../AP/s/components/c/**PortalHeader.js**:30:17*
Please note: 
  1.  I don't call it doInit I call it initMe function
  2.  even if I rename it to doInit I still get the same error.
  3.  There is no PortalHeader.js file there is PortalHeaderController.js and PortalHeaderHelper.js

Any idea of what is going on here and why am I getting this error?

Thank you very much!


 
Best Answer chosen by Al Be
Al BeAl Be
Fixed it!
Trick was to move all the initialization to the Helper file
And then call it once from doInit and again from the event
({
    doInit : function(component, event, helper) {
            helper.initHelper(component);

            window.addEventListener("message", function(event) {
                  helper.initHelper(component);
            });        
      },

 

All Answers

Abdul KhatriAbdul Khatri

Hi Alexei

Where are you coding?

  • Developer Console
  • Visual Code
It looks like you have the file and it is calling doInit method.

If you make changes in the component, make sure you also change in the Controller too.

Can you share the folder structure of your component?
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please use the below code. 

<aura:component>
    <aura:attribute name="setMeOnInit" type="String" default="default value" />
    <aura:handler name="init" value="{!this}" action="{!c.initMe}"/>
    
    <p>This value is set in the controller after the component initializes and before rendering.</p>
    <p><b>{!v.setMeOnInit}</b></p>
    
</aura:component>
 
({
    initMe: function(cmp) {
        // Set the attribute value. 
        // You could also fire an event here instead.
        cmp.set("v.setMeOnInit", "controller init magic!");
    }
})

Please mark it as the Best Answer if your queries are solved.

Thanks

Al BeAl Be
@Abdul Khatri  I work in Developer Console and Digital Experience Builder on this problem.
Yes I do have a call to doInit() method from Event listener (see below).
Problem is: doInit() is right there in the PortalHeaderController.
I can rename it ot initMe() in both places and get an error about initMe.
Also I should mention: error is happening in the Experience Builder, or when I add component to a page. Some times this error keeps popping up multiple times. I keep clicking [OK] and eventualy it goes away. and I can edit my page layout.
When I publish to the site or save the page, my component seems to be working correctly.
It seems to be a problem of the develpment tools.
Is there a way to correct it?
Thanks!
PortalHeaderController.js: 
({
    doInit : function(component, event, helper) {

        var action = component.get("c.fetchUser");
            action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") {
                    var storeResponse = response.getReturnValue();
                    component.set("v.userInfo",storeResponse);
                }
            });
            $A.enqueueAction(action);
        
         	window.addEventListener("message", function(event) {
                doInit();//<<<<#######
        	});
        
        },//end doInit---
...
})

 
Abdul KhatriAbdul Khatri
Hi Alexei

Where is the handler define for the doInit Method?

 
Al BeAl Be
@Abdul Khatri
In the PortalHeaderController.js
Al BeAl Be
Fixed it!
Trick was to move all the initialization to the Helper file
And then call it once from doInit and again from the event
({
    doInit : function(component, event, helper) {
            helper.initHelper(component);

            window.addEventListener("message", function(event) {
                  helper.initHelper(component);
            });        
      },

 
This was selected as the best answer