You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions
Description
Attempting to create a combined data source from 2 separate sources (contentful and markdown) and, from that, create image nodes to benefit from childImageSharp.gastbyImageData.
The combined data source is set up, however, when creating a node over a local image (i.e. not a remote file), the error "TypeError: Cannot set property 'contentDigest' of undefined" is returned when doing a gatsby develop.
My gatsby-node.js file contains the following:
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const fs = require('fs')
const moment = require('moment')
const validUrl = require('valid-url')
const crypto = require('crypto')
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest, cache, store, getNode, getNodesByType }) => {
const { createNode, createNodeField } = actions
// Fetch data from two data sources / collections
let newsItems = getNodesByType("ContentfulNews")
let articleItems = getNodesByType("MarkdownRemark")
if (articleItems.length > 0) {
articleItems = articleItems.filter(node => node.frontmatter.name === 'My articles')
}
// Merge the data from both collections
let allNews = []
let newsId = 0
newsItems.forEach(item => {
newsId = newsId + 1
const assetNode = getNode(item.image___NODE)
const imageUrl = assetNode ? `https:${assetNode.file.url}` : null
allNews.push({
id: createNodeId(`allnews-${newsId}`),
slug: item.slug,
title: item.title,
subtitle: item.subtitle,
date: moment(item.date).format('YYYY-MM-DD'),
imageArticle: null,
imageNews: imageUrl ? imageUrl : null,
})
})
let sectionId = 0
let articleId = 0
let imageArticleNode = ""
for (const article of articleItems) {
for (const section of article.frontmatter.section) {
sectionId = sectionId + 1
for (const item of section.feature) {
articleId = articleId + 1
imageArticleNode = ""
allNews.push({
id: createNodeId(`article-${sectionId}${articleId}`),
slug: item.link,
title: item.title,
subtitle: item.subtitle,
date: moment(item.date).format('YYYY-MM-DD'),
imageArticle: item.image ? path.join(__dirname, 'home/my-project', 'my-project', item.image) : null,
imageNews: null,
})
}
}
}
allNews.sort((a, b) => a.date == b.date ? 0 : a.date < b.date ? -1 : 1 )
let imageNodes = []
let imageExists = true
let imageId = 0
// Create a new node for each item
for (const item of allNews) {
imageId = imageId + 1
createNode({
id: item.id,
parent: null,
children: [],
internal: {
type: 'CompanyNews', // Name of the GraphQL type
contentDigest: createContentDigest(item),
},
// Data for the new node
slug: item.slug,
title: item.title,
subtitle: item.subtitle,
date: item.date,
imageArticle: item.imageArticle,
imageNews: item.imageNews,
})
}
}
exports.onCreateNode = async ({ node, getNode, actions, createNodeId, store, getCache, createContentDigest }) => {
const { createNode, createNodeField } = actions
if ( node.internal.type === "CompanyNews" && node.imageArticle !== null ) {
const fileNode = await createFileNode(node.imageArticle, createNodeId, {
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
actions,
createContentDigest,
})
if (fileNode) {
const nodeContent = JSON.stringify(fileNode)
const contentDigest = createContentDigest(fileNode)
fileNode.internal.contentDigest = contentDigest
createNodeField({ node, name: "image2", value: fileNode.id })
}
}
if ( node.internal.type === "CompanyNews" && node.imageNews !== null ) {
const fileNode = await createRemoteFileNode({
url: node.imageNews, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
getCache,
})
if (fileNode) {
createNodeField({ node, name: "localFile", value: fileNode.id })
}
}
}
async function createFileNode(imagePath, createNodeId, { parentNodeId, actions, createContentDigest }) {
const { createNode } = actions
try {
const fileNode = await createNode({
// Required fields
id: createNodeId(`${parentNodeId} >>> Image`),
parent: parentNodeId,
children: [],
internal: {
type: `ArticleImage`,
contentDigest: createContentDigest(fs.readFileSync(imagePath)),
mediaType: `image/${path.extname(imagePath).substr(1)}`, // or whatever type of file it is
},
sourceInstanceName: `images`,
absolutePath: imagePath,
// Optional fields
extension: path.extname(imagePath).substr(1),
// Add any other optional fields here
contentDigest: createContentDigest(fs.readFileSync(imagePath)),
})
return fileNode
} catch (e) {
console.error("Error creating file node:", e)
return null
}
}
createRemoteFileNode is working for remote images (contentful - imageNews field), however, createFileNode isn't working for markdown images over the imageArticle field which contains an absolute path (i.e. C:\home\my-project\static\assets\man-and-woman-holding-hands.jpg)
Reproduction Link
none
Steps to Reproduce
Source required data from contentful and markdown
Update gatsby-node.js file to include code above
Run gatsby develop
...
Expected Result
GraphQL schema is expected with fields.image2 present:
This discussion was converted from issue #38210 on June 12, 2023 04:30.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Preliminary Checks
Description
Attempting to create a combined data source from 2 separate sources (contentful and markdown) and, from that, create image nodes to benefit from childImageSharp.gastbyImageData.
The combined data source is set up, however, when creating a node over a local image (i.e. not a remote file), the error "TypeError: Cannot set property 'contentDigest' of undefined" is returned when doing a gatsby develop.
My gatsby-node.js file contains the following:
createRemoteFileNode
is working for remote images (contentful - imageNews field), however,createFileNode
isn't working for markdown images over the imageArticle field which contains an absolute path (i.e. C:\home\my-project\static\assets\man-and-woman-holding-hands.jpg)Reproduction Link
none
Steps to Reproduce
...
Expected Result
GraphQL schema is expected with fields.image2 present:
companyNews {
slug
date
image {
childImageSharp {
gatsbyImageData
}
}
imageArticle
fields {
image2
localFile
}
imageNews
}
Actual Result
GraphQL schema doesn't present fields.image2:
companyNews {
slug
date
image {
childImageSharp {
gatsbyImageData
}
}
imageArticle
fields {
localFile
}
imageNews
}
Environment
Config Flags
No response
Beta Was this translation helpful? Give feedback.
All reactions