+ Start a Discussion
nagachandra knnagachandra kn 

Input data in lightning component.

Hi All,

I am writing a basic lightning component. please find the code below. I am just getting the value from the inputtext component and displaying it on the console. But it is giving me following error. what i m doing wrong.?
 
<aura:component >
    <aura:attribute name="newExpense" type="KNNaga__Expense__c" access="public"/>
    
    <ui:inputText value="{!v.newExpense.KNNaga__Amount__c}" label="Expense Name" aura:id="Amount"/>
    <ui:button label="submit" press="{!c.createExpense}"/>
    
	
</aura:component>

controller.
({
	createExpense : function(component, event, helper) {
        
        var name = component.find("Amount");
        var value = name.get("v.value");
        
        console.log("value::"+value);
       
		
	}
})

Error which i m getting  aftter clicking the button. No error displayed in javascript console.

This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'KNNaga__Amount__c' of null] Failing descriptor: {markup://ui:input} 

Thanks in advance.

 
Best Answer chosen by nagachandra kn
sfdcMonkey.comsfdcMonkey.com
hi
give default value to attribute
<aura:component >
    <aura:attribute name="newExpense" type="KNNaga__Expense__c" default="{ 'sobjectType': 'KNNaga__Expense__c',
                                                                   'KNNaga__Amount__c': ,
                                                                   }" access="public"/>
    
    <ui:inputText value="{!v.newExpense.KNNaga__Amount__c}" label="Expense Name" aura:id="Amount"/>
    <ui:button label="submit" press="{!c.createExpense}"/>
    
	
</aura:component>
then your console display the value of input box
thanks
let me inform if it helps you
sfdcmonkey.com

All Answers

sfdcMonkey.comsfdcMonkey.com
hi
give default value to attribute
<aura:component >
    <aura:attribute name="newExpense" type="KNNaga__Expense__c" default="{ 'sobjectType': 'KNNaga__Expense__c',
                                                                   'KNNaga__Amount__c': ,
                                                                   }" access="public"/>
    
    <ui:inputText value="{!v.newExpense.KNNaga__Amount__c}" label="Expense Name" aura:id="Amount"/>
    <ui:button label="submit" press="{!c.createExpense}"/>
    
	
</aura:component>
then your console display the value of input box
thanks
let me inform if it helps you
sfdcmonkey.com
This was selected as the best answer
nagachandra knnagachandra kn
Hi Piyush,

Thanks u . it worked. One quick question.

Do u have specify "default" for each attribute i write ?
sfdcMonkey.comsfdcMonkey.com
hi nagachandra
we have not need to give default for each attribute its optional but in your case your input field  maping with Expense__c custom object so you must define default value if you only write like this (only object body) -:
<aura:attribute name="newExpense" type="Expense__c" default="{}" access="public"/>
then also your compnent work fine  . 
otherwise its assume object null.
so it always good pratice that when we are map any component field with custom or stand. object then give default value to attribute
 
sfdcMonkey.comsfdcMonkey.com
and you can learn basic thing in lightning by http://sfdcmonkey.com  (http://sfdcmonkey.com)also like page (https://www.facebook.com/SFDC-Monkeycom-1312920085424955/)for new post update :)