javascript - react native 错误 : Possible Unhandled Promise Rejection - Cannot read property data

标签 javascript react-native lodash

我遇到了一个奇怪的问题,我无法用我的 React Native 应用程序准确定位。我正在使用 Lodash 循环通过 API 响应,以在清除数组后使用返回的响应来设置位置数组的状态。当我在循环中仅显示响应的值/键时,它工作正常,但是当我尝试使用 setState 时,我可能会遇到未处理的 promise 错误。

这是循环和设置状态的代码:

    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

如有任何帮助,我们将不胜感激!

最佳答案

“_.forOwn”第二个参数中的 this 引用没有指向您的 React 类。 Javascript 函数不保留周围上下文的 this。你应该使用 ES6 箭头函数来保持这个上下文。此外,您的调用 this.setState({markers: []}) 也会导致问题。

_.forOwn(payload,(value, key) => {
            this.setState({
                markers: [
                    ...this.state.markers,
                    {
                        coordinate: key.coordinates,
                        key: id++
                    }
                ]
            })
        })

此外,this.setState 调用不是同步的,因此在您的 _.forOwn 回调中,无法保证 this.state.markers 可用。您应该重构您的代码,以便在您的 _.forOwn 调用之后仅使用新标记数组进行 setState 调用。

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)});

关于javascript - react native 错误 : Possible Unhandled Promise Rejection - Cannot read property data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39440789/

相关文章:

javascript - 用户ajax在javaScript中获取没有 "onClick function"的数据

javascript - 从日期对象数组中获取 id,并从日历中选择日期

javascript - 在 Tailwind CSS/NativeWind 中实现可变宽度

javascript - 如何使用存储在另一个数组中的值从数组中删除元素

javascript - 为什么 lodash _.includes 没有发现我的数组包含它正在检查的对象?

Javascript字符串插值给出与字符串连接不同的结果

javascript - 在 AngularJS 中完全加载页面后执行函数

java - 如何在 iOS 上的 Turbo 模块中发出事件

javascript - 将平面 json 转换为嵌套

javascript - 如何从 REST api 扩充数据