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?
tmp.getDataTable(onSuccessFunc.bind(this), onFailFunc.bind(this));to fix that.