-2

Here is my code:

function gotData(data) {
  console.log(data.val())
  var promotions = data.val()
  var keys = Object.keys(promotions)
  console.log(keys)
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i]
    var name = promotions[k].promotionName
    var description = promotions[k].description
    var validityFrom = promotions[k].validityPeriodFrom
    var validityTo = promotions[k].validityPeriodTo
    var dateCreated = promotions[k].dateCreated
    var dateUpdated = promotions[k].dateUpdated
  }
}

How can I access the variables name, description, validityFrom, validityTo, dateCreated and dateUpdated outside of my gotData function?

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • Return them. This is fundamental knowledge of almost every programming language. – Marty Sep 19 '17 at 06:04
  • `var myObj = {}` outside the function then `myObj.promotionName = promotions[k].promotionName` – Weedoze Sep 19 '17 at 06:04
  • https://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function This link will help you. – Anuj Masand Sep 19 '17 at 06:06
  • What are you trying to build here? Are you aware, that you overwrite all these values with every iteration of that loop? At first I thought you wanted to build something like PHPs [extract](http://php.net/extract), but now I'm confused – Thomas Sep 19 '17 at 06:35
  • Well, I'm using eslint so I don't use semi colons and double quotations sorry for the confusion. I want to pass in the data inside my variables to my table but my table is outside my function so I wanna know how can I access those variables to assign it to my table row – Jude Aquino Sep 19 '17 at 06:43

2 Answers2

0

You can't variables defined in the scope of a function are local to that function and cease to exist when you leave that function, if you want them to persist then create them outside the function, making them global or pass in an object and populate the object, using both these methods you can access the data modified inside the function.

    function gotData(data, aryResults) {
      if ( typeof aryResults != "object" ) {
        console.log("'aryResults' must be declared as []");
        return;
      }
      console.log(data.val());
      var promotions = data.val();
      var keys = Object.keys(promotions);
      console.log(keys);
      for( var i=0; i<keys.length; i++ ) {
        var k = keys[i];
        aryResults.push({name:promotions[k].promotionName
                 ,description:promotions[k].description
                ,validityFrom:promotions[k].validityPeriodFrom
                  ,validityTo:promotions[k].validityPeriodTo
                 ,dateCreated:promotions[k].dateCreated
                 ,dateUpdated:promotions[k].dateUpdated});
      }
    }

aryResults is an array which will contain all the extracted data.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
-1

you need to create a global variable and use it

var globalvar = [];

function gotData(data) {
  console.log(data.val())
  var promotions = data.val()
  var keys = Object.keys(promotions)
  console.log(keys)
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i]
    globalvar.name = promotions[k].promotionName
    globalvar.description = promotions[k].description
    globalvar.validityFrom = promotions[k].validityPeriodFrom
    globalvar.validityTo = promotions[k].validityPeriodTo
    globalvar.dateCreated = promotions[k].dateCreated
    globalvar.dateUpdated = promotions[k].dateUpdated
  }
}

then you can use this variables calling as below.

globalvar.name
globalvar.description
globalvar.validityFrom 
globalvar.validityTo
globalvar.dateCreated
globalvar.dateUpdated
lpradhap
  • 623
  • 6
  • 17