0

having an issue where after a server call and confirmed setting of the

getContentId : function(cmp, event) {

    var action = cmp.get("c.getConVerId");

    console.log(cmp.get("v.fileId"))

    action.setParams({ 
       "fId": cmp.get("v.fileId")
        });

   action.setCallback(this, function(response){
       var state = response.getState();
       if (state === "SUCCESS") {

           var returner = response.getReturnValue();
           console.log(returner);

           cmp.set('v.ContVerId',returner.Id)


           console.log(cmp.get('v.ContVerId'));


    }
});
   $A.enqueueAction(action);
   console.log(cmp.get('v.ContVerId'));
}

The first console.log(cmp.get('v.ContVerId')); shows the aura attribute as having been set to the intended content version Id. However the second console.log(cmp.get('v.ContVerId')); returns as undefined. I do not understand what is going on. Var returner is an object with a Id key value which i access and set to the aura attribute ContVerId which is a String attribute. Feel like i'm doing this all correctly but it is undefined even though initially it has been defined in the Callback

here is my apex class

@AuraEnabled
 public static ContentVersion getConVerId (string fId) {
    ContentVersion contverId = [SELECT Id FROM ContentVersion WHERE 
ContentDocumentId = :fId];       
    return contverId;
}

fId is a Content Document Id that i pass in

  • the action is being queued, while that happens, the value hasnt been set in your attribute as of yet, so when your "second" console log is geing executed, the value is still undefined – glls May 11 at 14:58
  • in a nutshell, the console log that you set after queueing the action is being called before – glls May 11 at 15:00
  • As a side note to @glls's answer, your SOQL query will throw an exception if your ContentDocument has more than one version. You'll want to query specifically for the latest version. – David Reed May 11 at 15:07
4

As mentioned in the documentation Calling a Server-Side Action

a callback is called after the server-side action is completed and $A.enqueueAction(action) adds the server-side controller action to the queue of actions to be executed.

This means that the value hasnt been set in your attribute as of yet, so, when your "second" console log is geing executed, the value is still undefined.

In a nutshell, the console log that you set after queueing the action is being called before.

  • That makes sense, through i am a little confused why the first console.log return a values. Either way I read through the documentation and understand the queueing. However the issue is i try to pass the attribute to another component (a component created with $A.createComponent) and since it is not defined at that point it passes as undefined. Any ideas on how to make sure the server side controller executes? – Bobbygllh May 12 at 2:27
  • 1
    @Bobbygllh You'd want to use a "promise chain" to make sure things happen in order. See this answer as an example of how to do that. – sfdcfox May 12 at 4:29

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.