Uncaught TypeError: Cannot read property ‘forEach‘ of undefined
程序员文章站
2022-06-07 12:35:41
...
js报错 Cannot read property 'forEach' of undefined
数组未定义或者不存在,解决方法:
if (datas && datas.length) {
datas.forEach(function (item, index) {
console.log(index);
})
}
// 或者
if (!datas || !datas.length) return;
datas.forEach(function (item, index) {
console.log(index);
})
也可以根据实际情况简写为
(datas || []).forEach(function (item) {
});
推荐阅读
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object
-
微信小程序报Cannot read property 'setData' of undefined的错误
-
webpack报错Cannot read property 'presetToOptions' of undefined
-
Extjs4---Uncaught TypeError: Cannot read property ‘items’ of undefined
-
JavaScript Uncaught TypeError: Cannot read property 'value' of null
-
Cannot read property 'tap' of undefined
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#
-
el-tooltip组件中content使用Vue-i18n报错TypeError: Cannot read property ‘$t‘ of null
-
vuejs路由使用的问题Error in render function: "TypeError: Cannot read property 'matched' of undefined"
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object