1

So, this is an error where I am kinda stuck with nowhere to go. I really donno how to resolve this as the code block looks all good to me. I am very new to lightning with only 2 days of lightning development experience, so please excuse me for any silly mistakes that I might have commited.

Here goes the component code:

<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="SearchContact">
<aura:attribute name="searchData" type="List"/>

<!--<lightning:input name="searchText"/>-->
<ui:inputText label="Enter the Search Text" value="Delete this and enter your keyword here" aura:id="{!v.searchText}" class="slds-input"/>
<lightning:button variant="brand" label="Search Contact" title="Search Contact" onclick="{!c.searchContact}" />

<lightning:card title="Contacts">
    <p class = "slds-p-horizontal_small">
        <aura:iteration items="{!v.searchData}" var = "contact">
            <lightning:recordviewForm recordId="{!contact.Id}" objectApiName="Contact">
                <lightning:outputField fieldName="Name"/>
                <lightning:outputField fieldName="Phone"/>
                <lightning:outputField fieldName="Email"/>
            </lightning:recordviewForm>
        </aura:iteration>
    </p>

</lightning:card>

Below is the Controller

({
searchContact : function(component, event, helper) {
    helper.fetchContacts(component, event, helper);
})

Helper :

({
fetchContacts : function(component, event, helper) {
    //debugger;
    var action = component.get("c.contactData");
    var searchString = component.get("v.searchText");

    //This is setting the value of the search keyword that is used in the apex controller
    action.setParams({
        keyword: searchString
    });


    action.setCallback(this, function(response){
        var ContactList = response.getReturnValue();
        component.set("v.searchData",ContactList);  
    });

//Global Action
$A.enqueueAction(action);    
}
})

My Apex code looks like the below:

public class SearchContact {

@AuraEnabled
public static List<Contact> contactData(String keyword){
    List<Contact> conData = [Select ID, Name, Phone, Email from Contact where FirstName =:keyword];
    return conData;
}
}

I created a lightning App to get the results of the above component:

<aura:application extends="force:slds">
<skmondal:MyContactSearch/>
</aura:application>

1 Answer 1

1

Main problem:

You must define an attribute that matches the name you're trying to use:

<aura:attribute name="searchText" type="String" />

Secondary problems:

<ui:inputText label="Enter the Search Text" value="Delete this and enter your keyword here" aura:id="{!v.searchText}" class="slds-input"/>

aura:id must be a literal string, not a merge field. value should be a merge field. If you want a "default text", consider using a placeholder.

<ui:inputText label="Search" value="{!v.searchText}" 
   placeholder="Enter a search term here" />

aura:id is not necessary here, since you're not trying to "find" the component.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .