I'm encountering an issue while trying to import a JSON file that contains an array of blog-posts. Although all the data from the JSON file is successfully imported, I am facing troubles with accessing the Array of objects (edges).
This specific code snippet is part of a unit test developed using JestJS for a Gatsby-based website.
Every time I attempt to access the edges array, I am presented with the error message "TypeError: Cannot read property 'edges' of undefined".
The JavaScript code in question looks like this:
import data from "./__mocks__/blog.json";
console.log(data);
data.allMarkdownRemark.edges.forEach((post) => {
console.log(post);
})
The output of the console logging the data variable is as follows:
{ data: { allMarkdownRemark: { edges: [Array] } }
The structure of my JSON file is already in JSON object format, therefore there is no need for JSON.parse() function. Here's how the JSON file appears:
{
"data": {
"allMarkdownRemark": {
"edges": [
{
"node": {
"id": "c60d0972-1f4a-55ae-b762-c24795fae501",
"fields": {
"slug": "/a-tu-cerebro-no-le-gusta-la-innovacion/"
},
"frontmatter": {
"title": "A tu cerebro no le gusta la Innovación",
"templateKey": "blog-post",
"date": "September 16, 2017"
}
}
},
{
"node": {
"id": "1624f260-4c77-55d3-8297-4f0ad688f878",
"fields": {
"slug": "/la-mente-es-para-tener-ideas-no-para-almacenarlas/"
},
"frontmatter": {
"title": "La mente es para tener Ideas™, no para almacenarlas",
"templateKey": "blog-post",
"date": "August 26, 2017"
}
}
}
]
}
}
}
Any insights on how to correctly import the "edges" array of objects from the JSON file?