0

I'm trying to get information about queries that I sent. I want to let users know if some results were found or not. But for some reason, I can't access the variable that contains the numbers of results found. I've also tried to define a global variable and also tried with window.variable but it just doesn't get updated inside the function. I was wondering what would be the best solution to this problem.

Here's my code:

   submitHandle() {
   //const r = 0; 
   //let r = 0; 
    //window.r = 0; // I have tried with all 3 declarations but still nothing
   Initialize sgvizler Query
  var Q = new window.sgvizler.Query(),

  onSuccessFunc = function (dataTable) {
   var r = dataTable.getNumberOfRows();

  }, 
 onFailFunc = function (datatable)
 {
   //handle error
 };

 var tmp = Q.query( " some query " )
 .endpointURL("some endpoint URL")
 .endpointOutputFormat("json");
 tmp.getDataTable(onSuccessFunc, onFailFunc);
 tmp.chartFunction("some chart function") 
 .draw(" div ID");

//handle states
   this.setState({ something: false });
  this.setState({ something: true });
  this.setState({ something: true });
}
 console.log(r) // <-- error = r is undefined.

The best solution would be to change some states inside of the onSuccessFunc but then if I try it then error Cannot read property 'setState' of undefined pops out, which is funny because I can access and change it if I put it where the other setStates are. So I guess that I'm trying to change the variable value inside a local variable and react just doesn't allow it. Any tricks which could help me fix the problem?

2
  • You have a context problem, you can use tmp.getDataTable(onSuccessFunc.bind(this), onFailFunc.bind(this)); to fix that. Commented Apr 18, 2018 at 7:10
  • Thank you @Titus!! Your suggestion fixed the problem. I didn't think of binding the function :) nice ! Commented Apr 18, 2018 at 7:17

1 Answer 1

0
onSuccessFunc().then(v=>console.log(r)).catch(v=>console.log('error',v))

It may work. and also check this link

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the suggestion, but it's not working as I expect it to.
what's the error ?
; expected. Besides fix was already posted. Thanks anyways.
use ; at last

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.