React Native错误:可能未处理的承诺拒绝-无法读取属性数据
React Native Error: Possible Unhandled Promise Rejection - Cannot read property data
我有一个奇怪的问题,我不能很精确地与我的React Native应用程序。我使用Lodash循环通过API响应设置位置数组的状态使用返回的响应清除数组后。当我在循环中只显示响应的值/键时,它工作得很好,但是当我尝试使用setState时,我得到一个可能的非手动承诺错误。
下面是循环和设置状态的代码: updateMarkers(payload) {
this.setState({
markers: []
})
let id = 0
_.forOwn(payload, function(value, key) {
this.setState({
markers: [
...this.state.markers,
{
coordinate: key.coordinates,
key: id++
}
]
})
})
console.log(this.state.markers)
}
下面是我收到的回复:
Possible Unhandled Promise Rejection (id: 0):
Cannot read property 'data' of undefined
TypeError: Cannot read property 'data' of undefined
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:113197:55
at tryCallOne (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25822:8)
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25908:9
at JSTimersExecution.callbacks.(anonymous function) (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8801:13)
at Object.callTimer (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8581:1)
at Object.callImmediatesPass (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8680:19)
at Object.callImmediates (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8695:25)
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7405:43
at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7283:1)
at MessageQueue.__callImmediates (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7405:1)
这是当我控制台记录值和键(不设置状态)时返回的对象的样子。正确循环遍历每个对象。
Object {coordinates: Object, description: "Machine F", distance: 1.220632249131323, inventory: Array[16], pid: "-KP6ulcT9qeDb1oFFrVA"}
coordinates:Object
description:"Machine F"
distance:1.220632249131323
inventory:Array[16]
pid:"-KP6ulcT9qeDb1oFFrVA"
__proto__:Object
任何帮助都将非常感激!
您的this
引用在'_。forOwn的第二个参数没有指向你的react类。Javascript函数不保留周围上下文的this
。您应该使用ES6箭头函数来保持此上下文。你的呼叫this.setState({markers: []})
也会引起问题。
_.forOwn(payload,(value, key) => {
this.setState({
markers: [
...this.state.markers,
{
coordinate: key.coordinates,
key: id++
}
]
})
})
也这样。setState调用不是同步的,所以在你的_。对于自己的回调,不能保证this.state.markers是可用的。你应该重构你的代码,这样只有当setState调用在你的_。forOwn电话。
var markers =[]
let id = 0
_.forOwn(payload, function(value, key) {
markers.push({
coordinate: key.coordinates,
key: id++
});
});
this.setState({markers},()=>{console.log(this.state.markers)});
相关文章:
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 在流星上使用微信js-sdk时出现拒绝权限错误
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 如何在Javascript中读取unsigned int
- 使用AJAX读取一个简单的文本文件.打开函数时出现拒绝访问错误
- 当尝试仅在 IE 上使用 ajax 请求读取本地 xml 文件时,访问将被拒绝
- JavaScript IE 从 json 文件中读取数据 访问被拒绝
- React Native错误:可能未处理的承诺拒绝-无法读取属性数据
- 如何在Firebase中使用js web API捕获读取权限拒绝