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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.