I have a simple code, but I receiving (sometimes) wrong results, it's because of NodeJS async, I know. Finally I need do update "this.setState({active: options})". So to solve the issue of async of NodeJS I used the require("async") library. But now my simple code got quite complex, and the result is also not 100% good.
And also at the on "function road" when i use the "this.setState({actove: options})", or "this.setState({actove: results.optionsFill})", I receive errors: "Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined" and "Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined".
Please see the simple start code (the code calculates the legend input to a map, the arr for number periods):
setLegend(data){
function roundingSize(number){
}
function rounding(number){
}
function stopsArrFill(jumpLegendValue, highestNumber){
}
// Searching for the highestNumber POPULATION
var highestNumber = 0
data.features.forEach(element => {
var value = element.properties["POPULATION"]
if(highestNumber<value) highestNumber = value
})
// Need to round the number, so it will look nice on the legend
highestNumber = roundingSize(highestNumber)
// Searching for the LowestNumber POPULATION
var LowestNumber = highestNumber
data.features.forEach(element => {
var value = element.properties["POPULATION"]
if(LowestNumber>value) LowestNumber = value
})
// Need to round the number, so it will look nice on the legend
LowestNumber = roundingSize(LowestNumber)
// 7 because 1 element is 0 and the last is the highestNumber, so it gives 7 empty elements to fill
var jumpLegendValue = Math.round(rounding((highestNumber - LowestNumber)/7))
var tmpStops = stopsArrFill(jumpLegendValue, highestNumber)
const options = {
name: 'Legenda',
description: 'Liczba wystąpień',
property: 'POPULATION',
stops: tmpStops
}
this.setState({active: options})
Now the complex code with async.auto:
setLegend(data){
function roundingSize(number){
}
function rounding(number){
}
function stopsArrFill(jumpLegendValue, highestNumber){
}
async.auto({
highestNumber: function(callback){
// Searching for the highestNumber POPULATION
var highestNumber = 0
data.features.forEach(element => {
var value = element.properties["POPULATION"]
if(highestNumber<value) highestNumber = value
})
// Need to round the number, so it will look nice on the legend
highestNumber = roundingSize(highestNumber)
callback(null, highestNumber)
},
lowestNumber: ['highestNumber', function(results, callback){
// Searching for the LowestNumber POPULATION
var lowestNumber = results.highestNumber
data.features.forEach(element => {
var value = element.properties["POPULATION"]
if(lowestNumber>value) lowestNumber = value
})
// Need to round the number, so it will look nice on the legend
lowestNumber = roundingSize(lowestNumber)
callback(null, lowestNumber)
}],
jumpLegendValue: ['highestNumber', 'lowestNumber', function(results, callback){
// 7 because 1 element is 0 and the last is the highestNumber, so it gives 7 empty elements to fill
var jumpLegendValue = Math.round(rounding((results.highestNumber - results.lowestNumber)/7))
callback(null, jumpLegendValue)
}],
stopsArrFill:['highestNumber','jumpLegendValue', function(results, callback){
// Filling the stops with jumpLegendValues
var tmpStops = stopsArrFill(results.jumpLegendValue, results.highestNumber)
callback(null, tmpStops)
}],
optionsFill:['stopsArrFill', function(results, callback){
const options = {
name: 'Legenda',
description: 'Liczba wystąpień',
property: 'POPULATION',
stops: results.stopsArrFill
}
callback(null, options)
}],
function (err, results)
{
this.setState({active:results.optionsFill})
console.log('error: ', err)
}
})
I am now playing around with this for 2 days, do you have any ideas / suggestions? I couldn't find any similar usage for setState with async.auto.
To solve the setState issue, you need to be aware that the async.auto({}) is a promise.
The final error function need to be deleted this code:
function (err, results)
{
this.setState({active:results.optionsFill})
console.log('error: ', err)
}
and at the end of async.auto({}) need to add:
.then((result)=>{
this.setState({active: result.optionsFill})
})