0

I am getting a NaN value for response.getReturnValue() and data is not being stored. What exactly I need to do to identify an error message

This is my controller

({
doSubmit : function(cmp, evt, hlpr) {

    var regForm=cmp.get("v.RegForm_DB");

    var action=cmp.get("c.Save_Reg_Details");
    action.setParams({RegForm_DB: regForm});

    //Create a call back that is executed after
    //server side action returns
    action.setCallback(this,function(response){
        var state=response.getState();
        if(state === "SUCCESS"){
            //Alert the user with value
            //returned from the user
            alert("From server: ",response.getReturnValue());
            console.info('response', response);
            console.log("From server: ",+response.getReturnValue());
        }
        else if(state === "INCOMPLETE"){
            console.log("iNCOMPLETE message: ");
        }
            else if(state === "ERROR"){
                var errors=response.getError();
                if(errors){
                    if(errors[0] && errors[0].message){
                        console.log("Error message: ",+ errors[0].message);
                    }
                }
                else{
                    console.log("Unknown error");
                }
            }
        });

    $A.enqueueAction(action);

}

This is my Apex Class

public class Save_Registration_Details {

@AuraEnabled
public static id Save_Reg_Details(learning123__Registration_Details__c RegForm_DB){
    //inserting DML Operation
   insert RegForm_DB;
    return RegForm_DB.id;
}

}

This is my Console Log (How and where to find an exact error in response) enter image description here

This is my Component Class

<aura:component controller="Save_Registration_Details" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <!-- Defining Attributes -->
<aura:attribute name="abc" type="string" default="false" />
<aura:attribute name="NameOfStudent" type="String" default="your name"/>
<aura:attribute name="NameOfFather" type="String" default="father name"/>
<aura:attribute name="ClassStudying" type="String[]" default="Select One,Jr KG,Sr KG,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten"/>
<aura:attribute name="ClassResponse" type="String" default="class name" />
<aura:attribute name="Section" type="String[]" default="A,B,C"/>
<aura:attribute name="SectionResponse" type="String" default="sec name" />
<aura:attribute name="PhoneNumber" type="integer" default="0"/>
<aura:attribute name="NoOfHoursStudy" type="String[]" default=",Less than 2 Hour,In Between 2 to 4 Hours,In Between 4 to 8 Hours,In Between 8 to 10 Hours,10 + Hours"/>
<aura:attribute name="HourResponse" type="String" default="study hrs" />
<aura:attribute name="HaveYouTakenClass" type="Boolean" default="false"/>
<aura:attribute name="AreYouInterestedInOnline" type="Boolean" default="false"/>
<aura:attribute name="EducationDetailsList" type="List" default="[]"/>
<aura:attribute name="RegForm_DB" type="learning123__Registration_Details__c" default="{'sobjectType': 'learning123__Registration_Details__c'}" />
<!--Attributes defining is done -->

<!--U/I Starts here  -->
<br/><br/> 
<div class="slds-size_3-of-8">
 <p>
<lightning:input  label="Enter your name" name="studentname" value="{!v.RegForm_DB.Name}" />


<lightning:select label="Which class you are studying?" name="ClsStudying" value="{!v.RegForm_DB.learning123__Class_Studying__c}">
  <aura:iteration items="{!v.ClassStudying}" var="cls">
   <option value="{!cls}" text="{!cls}" />
    </aura:iteration> 
</lightning:select>

<lightning:select label="Please select your section?" name="SecResponse" value="{!v.RegForm_DB.learning123__Section__c}">
<aura:iteration items="{!v.Section}" var="sec">
    <option value="{!sec}" text="{!sec}" />
    </aura:iteration>
</lightning:select>

    <lightning:select label="How many hours you want to study?" name="hrsStudy" value="{!v.RegForm_DB.learning123__Hours_Study__c}">
    <aura:iteration items="{!v.NoOfHoursStudy}" var="hrs">
        <option value="{!hrs}" text="{!hrs}" />
 </aura:iteration>
</lightning:select>

<lightning:input type="telephone" label="Enter your telephone number" value="{!v.RegForm_DB.learning123__Mobile_Number__c}" />

     <br/>

     <lightning:button iconName="utility:add" label="Add Educational Details" onclick="{!c.addDetails}"/>


<lightning:input aura:id="checkCrs" label="Are you intrested Online?" type="checkbox" name="crsCheck" 
                 onchange="{!c.onCheckBox}"  />


        <aura:if isTrue="{!v.AreYouInterestedInOnline}" >
Have a great learning!
</aura:if>
    <br/>
     <lightning:button label="Submit" onclick="{!c.doSubmit}"/>

</div>

This is my Registration_Detail Object and the field relationships

enter image description here

2

You are't getting back NaN, which would be quite odd. Your server controller method will either return an Id or throw an exception.

You have a couple of bugs in your JavaScript.

        alert("From server: ",response.getReturnValue());
        console.info('response', response);
        console.log("From server: ",+response.getReturnValue());

alert() does not take two parameters, so response.getReturnValue() is never shown to you.

Your console.log() statement has either a spurious comma or a spurious plus (you need to pick one or the other). This results in response.getReturnValue() being interpreted in a numeric sense, but it's not a number.

You can illustrate this yourself by doing

console.log('Test', +'blah');

in a JavaScript console. You'll get back

Test NaN

The fact that your console.log() messages are showing at all implies your server controller method is succeeding, since those messages are logged in the 'SUCCESS' branch of your logic.

1
  • Thanks, David! I am able to see a record with the response code. I have removed all the bugs which you have mentioned
    – Pran
    Dec 25 '18 at 22:50

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.