javascript - 未定义错误的属性在刷新时消失

标签 javascript reactjs undefined

在我的项目启动时,我收到一条错误,指出 TypeError: Cannot read property 'ActionType' of undefined 这仅在我第一次打开项目时发生。当我刷新页面时,错误消失并且不会再出现。

const details = {
 ActionType: currentData.ActionType,
 RequestedByName: currentData.RequestedByName
}

{
            name: "",
            label: "",
            options: {
                filter: false,
                sort: false,
                customBodyRender: (value, tableMeta) => {
                       const currentData= opt [
                            tableMeta.rowIndex
                        ];
                        const details = {
                            ActionType: currentData.ActionType,
                            RequestedByName: currentData.RequestedByName
                        }
                    return (
                        currentData.ActionTypeID === 0 || currentData.ActionCompletedByID > 0 ? null :
                        <Icon>
                        <Modal
                        details = {details}>
                    </Modal>
                    </Icon> 
                        )
                }
            }
        },

最佳答案

检查 opt 数组的值,具体来说,opt[tableMeta.rowIndex](添加 console.log 或断点)。当您第一次访问该页面时,该值很可能是未定义,因此访问该页面上的属性会导致错误。

我建议在分配给 details 之前添加一个 if(currentData)... 条件。

关于javascript - 未定义错误的属性在刷新时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200753/

相关文章:

javascript - Ember Data 从存储中抓取已加载的对象

javascript - 如何解析框架集中页面之间调用的 URL?

javascript - Bootstrap 行导致 FullPageJS 的宽度和高度错误

css - 覆盖样式组件 3 次?

javascript - 确认 Material 表中的自定义操作 [React]

javascript - 对象中的 null 和 undefined

地理 IP 查找上的 Perl 未定义错误

javascript - 我需要在 <li> 标签上与不同的 <ul> 具有相同的高度

node.js - 一个 Package.json 来管理 Node 和 React 依赖项

javascript - forEach 在 Array 构造函数创建的未定义数组上