So I have a global variable defined in my code and it is throwing an error. The error is

Failed to save undefined: 0Ad1F0000000meq: org.auraframework.util.json.JsonStreamReader$JsonStreamParseException: Only functions are allowed in javascript controllers [3, 17]: '10': Source

My code is in JavaScript. It is a controller for my component. I want to declare a global variable but am I declaring it correctly?

Simplified code:

  x: 10,   /* Global variable */
  y: 15    /* Global variable */

  function1 : function(..){
  • you cannot do this as you are attempting it in your lightning controller, the message is pretty straightforward : Only functions are allowed in javascript controllers. have you gone through the documentation by any chance? or do you have a reference that states that this should work? – glls Sep 20 '17 at 12:43
  • I'm looking at the documentation now. – M guy Sep 20 '17 at 12:48
  • 1
    :thumbsup: the trailheads on building lightnign components should give you a global understanding of how the aura framework and lightning work to help you develop – glls Sep 20 '17 at 12:49
  • @Mguy, my answer is incorrect, please mark the other answer as correct. :) – Raul Aug 5 '20 at 16:46

You can declare those variables in helper and access them like that:

    x: 10,
    y: 15,
    someHelperFunction: function() {

or from the controller:

    someControllerFunction: function(component, event, helper) {

That's not possible within lightning framework.

Unlike JavaScript, we have to create aura:attribute in component; which can be used for storing data available to client side controller and helper globally.

Basic example


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

    // other code

With this recordId attribute is registered for the component and can be accessed by v.recordId property in client side helper and controller.

// getter
// setter
component.set("v.recordId", "record id value");

One can use a function in helper or controller which returns value but using aura:attribute is much cleaner.

  • 1
    Incorrect. See the answer below. Also, when you do set attribute values, it triggers a re-render which is inefficient at best, and can cause infinite looping issues at worst. – Shane Aug 5 '20 at 16:44
  • 2
    Yes you're right, I learnt this at a later point. I am not sure how I can ask op to choose the other right answer. I guess will leave comment to the question. Thanks for notifying me and correcting. – Raul Aug 5 '20 at 16:46
  • Cool man. Maybe ask the OP to select the other answer as correct? Its ideal for others looking for this to see the best answer of course! Cheers. – Shane Aug 5 '20 at 18:42

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.