1

I am getting value in JSON format in a controller function and want to set it to attribute in it's associated component.

controller function

userFeedItem: function(component, event, helper) {
    var userFeed = component.get("c.getFeedData");
    var groupID = component.get("v.groupattid");
    console.log("GroupID: " + groupID);
    userFeed.setParams({"groupId" : groupID});
    userFeed.setCallback(this,function(response){
    var state = response.getState();
        if(state==="SUCCESS"){
          var jsontext = JSON.stringify(response.getReturnValue(),null,4);
         // var jstext = JSON.parse(jsontext);
           //console.log("USERS_BODY:3 " + JSON.stringify(response.getReturnValue(), null, 4));
    //console.log("USERS_BODY:4 " + jstext);            
    component.set("v.groupatt",jsontext);
        } else {
            console.log('Problem getting feed, response state: ' + state);
        }    
    });
    $A.enqueueAction(userFeed);
}

Component

<aura:component controller ="ChatterFeedController">
     <aura:attribute name = "groupatt" type="Object" /> 
     <aura:handler name="init" action="{!c.userFeedItem}" value="{!this}"/> 

     <div class ="slds">
     <!--Page Header-->
     <div class="slds-page-header">
        <div class="slds-grid">
        <span class="big">THOR Announcement</span>
        </div>
      </div>
    <section class="slds-card__body">
    <div id = "List" class="row">           
       <aura:iteration items="{!v.groupatt}" var="att">
            <span class="slds-truncate">{{display a value from the JSON}}
            </span>
        </aura:iteration> 
     </div> 
     </section>

    </div>
</aura:component>
1
  • 2
    Please take a moment to visit the help center, scroll through the tour, and read How to Ask. As written, your post does not contain a question. What is the expected versus observed behavior? Do you have any error messages? Have you written any code which you can include? Please edit your post to be more detailed.
    – Adrian Larson
    Apr 10, 2017 at 21:09

1 Answer 1

5

Since you have defined an Object attribute you need to set an object to it from your controller.

Depending on your return value from your Apex controller, you could possibly directly set the value:

component.set("v.groupatt",response.getReturnValue());

Or if it's a JSON string coming back from the controller, Parse that instead:

component.set("v.groupatt",JSON.parse(response.getReturnValue()));

Note, I'd probably add a bit more error checking than I have here, but this is just for example purposes.

Do not JSON.stringify() anything. You do not want a string.

You must log in to answer this question.

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