迭代对象-无法读取未定义的属性

时间:2019-03-30 21:07:52

标签: javascript reactjs redux

我有问题。

此命令显示此值 enter image description here

如果您想从photos对象中调用某些内容,则会收到错误消息。

console.log(data.photos.total)
UserProfilGallery.js:17 Uncaught TypeError: Cannot read property 'total' of undefined

为什么会这样?我找不到此错误的原因

1 个答案:

答案 0 :(得分:1)

我假设您在控制台中在数据之前看到undefined。那是因为data.photos当时确实是未定义的。添加至少可以解决该错误的检查:如果data.photos && data.photos.total有一个值,则data.photos将以这种方式访问​​–将访问total属性。