You need to sign in to do that
Don't have an account?
Padma Sree 39
Getting error while calling doInit from Lightning Component
Hi,
I am getting the below exception.
This page has an error. You might just need to refresh it. Unable to find action 'doInit' on the controller of c:contactRecordDispaly Failing descriptor: {c:contactRecordDispaly}
<aura:component controller="contactRecordData" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="conts" type="Contact[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<table>
<th>
<div title="Name">Contact Name</div>
</th>
<th>
<div title="Id">Contact Id</div>
</th>
<th>
<div title="Phone">Phone</div>
</th>
<th>
<div title="Email">Email</div>
</th>
<aura:iteration items="{!v.conts}" var="cnt" >
<tr>
<div>{!cnt.name}</div>
<div>{!cnt.Id} </div>
<div>{!cnt.Phone}</div>
<div>{!cnt.Email}</div>
</tr>
</aura:iteration>
</table>
</aura:component>
public class contactRecordData {
@AuraEnabled
public List<Contact> getContactList(){
List<Contact> conList=[select id,FirstName,LastName,Email,Phone from Contact];
return conList;
}
}
({
doInit : function(component, event, helper) {
var action = component.get("c.getContactList");
action.setCallback(this, function(response)
{
var state=response.getState()
if(state==="SUCCESS")
{
var Results=response.getReturnValue();
component.set("v.conts",Results);
}
});
$A.enqueueAction(action);
}
})
I am getting the below exception.
This page has an error. You might just need to refresh it. Unable to find action 'doInit' on the controller of c:contactRecordDispaly Failing descriptor: {c:contactRecordDispaly}
<aura:component controller="contactRecordData" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="conts" type="Contact[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<table>
<th>
<div title="Name">Contact Name</div>
</th>
<th>
<div title="Id">Contact Id</div>
</th>
<th>
<div title="Phone">Phone</div>
</th>
<th>
<div title="Email">Email</div>
</th>
<aura:iteration items="{!v.conts}" var="cnt" >
<tr>
<div>{!cnt.name}</div>
<div>{!cnt.Id} </div>
<div>{!cnt.Phone}</div>
<div>{!cnt.Email}</div>
</tr>
</aura:iteration>
</table>
</aura:component>
public class contactRecordData {
@AuraEnabled
public List<Contact> getContactList(){
List<Contact> conList=[select id,FirstName,LastName,Email,Phone from Contact];
return conList;
}
}
({
doInit : function(component, event, helper) {
var action = component.get("c.getContactList");
action.setCallback(this, function(response)
{
var state=response.getState()
if(state==="SUCCESS")
{
var Results=response.getReturnValue();
component.set("v.conts",Results);
}
});
$A.enqueueAction(action);
}
})
Please refresh your page more than twice. Also please add static keyword near public like below,
public with sharing class contactRecordData {
@AuraEnabled
public static List<Contact> getContactList(){
List<Contact> conList=[select id,FirstName,LastName,Email,Phone from Contact];
return conList;
}
}
Please try it and let me know.. And mark it as best answer...
Please replace your code with given below code with the help of this code you can solve your problem, it may be helpful to you.
Lightning component:
<aura:component controller="contactRecordData" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="conts" type="Contact[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<table>
<th>
<div title="Name">Contact Name</div>
</th>
<th>
<div title="Id">Contact Id</div>
</th>
<th>
<div title="Phone">Phone</div>
</th>
<th>
<div title="Email">Email</div>
</th>
<aura:iteration items="{!v.conts}" var="cnt">
<tr class="slds-hint-parent">
<td data-label="{!cnt.LastName}">
<div class="slds-truncate" title="{!cnt.LastName}">{!cnt.LastName}</div>
</td>
<td data-label="{!cnt.Id}">
<div class="slds-truncate" title="{!cnt.Id}">{!cnt.Id}</div>
</td>
<td data-label="{!cnt.Phone}">
<div class="slds-truncate" title="{!cnt.Phone}">{!cnt.Phone}</div>
</td>
<td data-label="{!cnt.Email}">
<div class="slds-truncate" title="{!cnt.Email}">{!cnt.Email}</div>
</td>
</tr>
</aura:iteration>
</table>
</aura:component>
Lightning js Controller:
({
doInit : function(component, event, helper) {
try{
console.log('Inside doInit');
var action = component.get("c.getContactList");
action.setCallback(this, function(response)
{
var state=response.getState()
if(state==="SUCCESS")
{
var Results=response.getReturnValue();
component.set("v.conts",Results);
}
});
$A.enqueueAction(action);
}catch(ex){
console.log('Exception:>>>'+ex);
}
}
})
Apex Controller:
public class contactRecordData {
@AuraEnabled
public static List<Contact> getContactList() {
try {
List<Contact> conList = [select Id, FirstName, LastName, Email, Phone FROM Contact LIMIT 1000];
System.debug('conList>>>>' + conList);
if(!conList.isEmpty()){
return conList;
}
} catch (Exception ex) {
System.debug('Exception: ' + ex.getMessage() + ' At Line No: ' + ex.getLineNumber());
}
return null;
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Try this code:
Component:
<aura:component controller="contactRecordData" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="conts" type="Contact[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<table>
<th>
<div title="Name">Contact Name</div>
</th>
<th>
<div title="Id">Contact Id</div>
</th>
<th>
<div title="Phone">Phone</div>
</th>
<th>
<div title="Email">Email</div>
</th>
<aura:iteration items="{!v.conts}" var="cnt">
<tr>
<td>
<div>{!cnt.LastName}</div>
</td>
<td>
<div>{!cnt.Id}</div>
</td>
<td>
<div>{!cnt.Phone}</div>
</td>
<td>
<div>{!cnt.Email}</div>
</td>
</tr>
</aura:iteration>
</table>
</aura:component>
Controller:
public class contactRecordData {
@AuraEnabled
public static List<Contact> getContactList(){
List<Contact> conList = [select Id, FirstName, LastName, Email, Phone FROM Contact LIMIT 1000];
return conList;
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi