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

Create a script which uses the "forEach" method. png Json is an array of objects with the following sequence:

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

gets error: 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);
    });


Solution 1:[1]

if your Json is

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

shouldn't it be pngJson.data.forEach instead of pngJson.png.forEach?...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonastro M