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
LVL 2
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
I am willing to bet that your
tableService.queryEntities
Is an ASYNCH function that calls the passed function as a callback.

So what is happening is you are setting DataFound to NO
Calling queryEntities
This takes a LOOOOOOONG time to complete
While it is busy your code continues and dumps the DataFound value - which is still NO
Years (comparatively speaking) later the ASYNC function completes and sets the DataFound to yes - except everyone has gone home so there is noone to see the return.

You need to do the stringify IN the callback function

In other words here
    , 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);

          // THIS IS WHERE YOU PROCESS THE RETURN OF THE FUNCTION
          var oDataJson = JSON.stringify({DataExists: DataFound});
          // rest of code that depends on this return
        }
      }
     ...
     // THIS IS MEANINGLESS AS THE CALL IS STILL BUSY AT THIS POINT
     return oDataJson;

Open in new window

0
 
Ryan ChongCommented:
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

0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
I tried that but it did not work,

What happens it just always defaults to NO

But looking at the log file I know
0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
that the value is showing as DataFound 2="YES"
0
 
leakim971PluritechnicianCommented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.