Link to home
Start Free TrialLog in
Avatar of Jayesh Acharya
Jayesh AcharyaFlag for United States of America

asked on

javascript variable ReferenceError: DataFound is not defined

I have created a javascript function but I have having trouble with a variable that is not aviable to me when later on in the function
..
..
 tableService.queryEntities
  (   'MyTable'
    , tableQuery
    , null
    , function(error, result, response) 
    {
      if (!error)
      {  
        if (result.entries.length == 0)
        {
          var DataFound = "NO";
          context.log("DataFound 1= " +DataFound);
          context.log(result.entries.length );
          
        }       
        else
        {
          var DataFound = "YES";
          context.log("DataFound 2= " +DataFound);

        }
      }
      else
      {
        var DataFound = "NO";
        context.log("DataFound 3= " +DataFound);
        context.log("Error retrieving entity:");
        context.log(new Error(error));
        context.log(response);

      } 
    }
  );
 
  var oDataJson = JSON.stringify({DataExists: DataFound});
  context.log(oDataJson)
  return oDataJson;
}

Open in new window



now I know that the  tableService.queryEntities piece works as the log file shows me a value of
DataFound 2= YES

but I get an error at the line
  var oDataJson = JSON.stringify({DataExists: DataFound}

Open in new window

);

its complaingin that it cant find the value DataFound.

now I tried to define this variable outside of the tableService.queryEntities , but then what I got was the value that I intialized DataFound.

so I need some help in making sure I can pass this value of YES or NO  that is contained in "DataFound" to the calling function
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try define var DataFound in a bigger scope, which mean outside of tableService.queryEntities.

like:

..
..

var DataFound = "NO";

 tableService.queryEntities
  (   'MyTable'
    , tableQuery
    , null
    , function(error, result, response)
    {
      if (!error)
      {  
        if (result.entries.length == 0)
        {
          DataFound = "NO";
          context.log("DataFound 1= " +DataFound);
          context.log(result.entries.length );
         
        }      
        else
        {
          DataFound = "YES";
          context.log("DataFound 2= " +DataFound);

        }
      }
      else
      {
        DataFound = "NO";
        context.log("DataFound 3= " +DataFound);
        context.log("Error retrieving entity:");
        context.log(new Error(error));
        context.log(response);

      }
    }
  );
 
  var oDataJson = JSON.stringify({DataExists: DataFound});
  context.log(oDataJson)
  return oDataJson;
}

Open in new window

Avatar of Jayesh Acharya

ASKER

I tried that but it did not work,

What happens it just always defaults to NO

But looking at the log file I know
that the value is showing as DataFound 2="YES"
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you may use Promise to let your code more readable :

if you need help, share more of you code (should OK with two functions at first look)