简体   繁体   English

TypeError: Cannot read properties of undefined (reading 'forEach') 如何解决这个问题?

[英]TypeError: Cannot read properties of undefined (reading 'forEach') How to overcome this problem?

Create a script which uses the "forEach" method.创建一个使用“forEach”方法的脚本。 png Json is an array of objects with the following sequence: png Json 是一个对象数组,其顺序如下:

      "uri": ".....",
      "id": "0222 2.PNG",
      "type": "IMG"
    },

gets error: pngJson.png.forEach((element) => { TypeError: Cannot read properties of undefined (reading 'forEach')得到错误:pngJson.png.forEach((element) => { TypeError: Cannot read properties of undefined (reading 'forEach')

The debugger reads the file correctly until it fails调试器正确读取文件,直到失败

Here is a code snippet:这是一个代码片段:

  let pngMap = new Map();
    let pngJson = JSON.parse(fs.readFileSync(staticJson));

    pngJson.png.forEach((element) => {
        pngMap.set(element.id, element.uri);
    });

if your Json is如果你的 Json 是

{ "data": [ { "uri": "QmNvzUyXezTMorSXTrFnAyBUJrH6K41GsWqeS", "id": "f.PNG", "type": "10" } ] }

shouldn't it be pngJson.data.forEach instead of pngJson.png.forEach ?...不应该是pngJson.data.forEach而不是pngJson.png.forEach吗?...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取'forEach) - TypeError: Cannot read properties of undefined (reading 'forEach) TypeError: Cannot read properties of undefined (reading 'strEmail') 我该如何解决这个问题? - TypeError: Cannot read properties of undefined (reading 'strEmail') How can I solve this problem? TypeError:无法读取未定义的属性(读取“查询”) - TypeError: Cannot read properties of undefined (reading 'query') TypeError:无法读取未定义的属性(读取“_id”) - TypeError: Cannot read properties of undefined (reading '_id') NodeJS TypeError:无法读取未定义的属性(读取“0”) - NodeJS TypeError: Cannot read properties of undefined (reading '0') TypeError:无法读取未定义的属性(读取“名字”) - TypeError: Cannot read properties of undefined (reading 'firstname') TypeError:无法读取未定义的属性(读取“使用”) - TypeError: Cannot read properties of undefined (reading 'use') TypeError:无法读取未定义阅读“电子邮件”的属性 - TypeError: Cannot read properties of undefined reading 'email' TypeError:无法读取未定义的属性(读取“配置”) - TypeError: Cannot read properties of undefined (reading 'config')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM