Rejection (TypeError): Cannot read property 'setState' of undefined, Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined
Rejection (TypeError): Cannot read property 'setState' of undefined, Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined
我有一个简单的代码,但我收到(有时)错误的结果,这是因为 NodeJS 异步,我知道。最后我需要做更新 "this.setState({active: options})"。因此,为了解决 NodeJS 的异步问题,我使用了 require("async") 库。但是现在我的简单代码变得相当复杂,结果也不是 100% 好。
并且在 "function road" 上,当我使用 "this.setState({actove: options})" 或 "this.setState({actove: results.optionsFill})" 时,我收到错误消息:"Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined" 和 "Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined"。
请看简单的开始代码(代码计算输入到地图的图例,数字周期的arr):
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})
现在复杂的代码用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)
}
})
我现在已经玩了 2 天了,你有什么想法/建议吗?我找不到 setState 与 async.auto 的任何类似用法。
要解决 setState 问题,您需要注意 async.auto({}) 是一个承诺。
最终错误函数需要删除这段代码:
function (err, results)
{
this.setState({active:results.optionsFill})
console.log('error: ', err)
}
并且在async.auto({})的末尾需要添加:
.then((result)=>{
this.setState({active: result.optionsFill})
})
我有一个简单的代码,但我收到(有时)错误的结果,这是因为 NodeJS 异步,我知道。最后我需要做更新 "this.setState({active: options})"。因此,为了解决 NodeJS 的异步问题,我使用了 require("async") 库。但是现在我的简单代码变得相当复杂,结果也不是 100% 好。
并且在 "function road" 上,当我使用 "this.setState({actove: options})" 或 "this.setState({actove: results.optionsFill})" 时,我收到错误消息:"Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined" 和 "Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined"。
请看简单的开始代码(代码计算输入到地图的图例,数字周期的arr):
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})
现在复杂的代码用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)
}
})
我现在已经玩了 2 天了,你有什么想法/建议吗?我找不到 setState 与 async.auto 的任何类似用法。
要解决 setState 问题,您需要注意 async.auto({}) 是一个承诺。
最终错误函数需要删除这段代码:
function (err, results)
{
this.setState({active:results.optionsFill})
console.log('error: ', err)
}
并且在async.auto({})的末尾需要添加:
.then((result)=>{
this.setState({active: result.optionsFill})
})