英文:
gatsby contentful rich text raw is undefined
问题
在Gatsby中尝试创建一个简单的网站。在我的特定示例中,我有一个带有来自Contentful的富文本字段的节点。它只是一个普通的文本句子。
{renderRichText(event.description.raw)}
然而,上面的调用返回:
在控制台中,我的event.description
看起来像这样:
description: raw: "{\"data\":{},\"content\":[{\"data\":{},\"content\":[{\"data\":{},\"marks\":[],\"value\":\"Hard beats. Fast music. Sweaty bodies. \",\"nodeType\":\"text\"}],\"nodeType\":\"paragraph\"}],\"nodeType\":\"document\"}"
我可以看到这是一个字符串,我尝试使用JSON.parse和JSON.stringify来处理它,然后发送给renderRichText
函数。但我无法确定问题出在哪里。它是一个字符串,但无法将其视为JSON对象。我还验证了它是有效的JSON。
event.js
const Event = ({ pageContext }) => {
const { event } = pageContext
console.log('grabbing event', event)
const image = event.flyer
return (
<>
<h2 className={styles.EventTitle}>{event.title}</h2>
<div className={`row`}>
<div className={` ${styles.EventInfo}`}>
{renderRichText(event.description.raw)}
</div>
</div>
<div className={`row`}>
<img className={styles.EventImage} src={image}/>
<div className={` ${styles.EventActions}`}>
<Link to={event.ticket}><button className={styles.EventButton}>Tickets</button></Link>
</div>
</div>
</>
)
};
export default Event;
gatsby-node.js
const path = require("path")
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const queryContentfulResults = await graphql(`
query Events {
allContentfulEvent {
nodes {
contentful_id
title
starttime
id
ad_mats {
file {
url
}
}
square_flyer {
file {
url
}
}
ticketlink
slug
venue {
name
street_address
city
}
description {
raw
}
}
}
}
`)
const eventTemplate = path.resolve(`src/templates/event.js`);
contentfulEvents = queryContentfulResults.data.allContentfulEvent.nodes.map(data => transformEvent(data));
contentfulEvents.forEach(node => {
console.log(node);
createPage({
path: `/events/${node.slug}`,
component: eventTemplate,
context: {
event: node
}
});
});
};
const transformEvent = (data) => {
console.log(data);
return {
title: data.title,
description: data.description,
slug: data.slug,
ad_mats: data.ad_mats,
flyer: data.square_flyer.file.url,
ticket: data.ticketlink,
date: data.starttime,
venue: data.venue.name,
address: data.venue.street_address,
city: data.venue.city
}
};
在将其发送给renderRichText
函数之前,尝试将其作为字符串和JSON进行处理,添加了一个空的选项对象,并验证了JSON。但是从这些操作中没有看到任何变化。
英文:
In Gatsby trying to make a simple website.
In my particular example I have a node with a rich text field from Contentful.
It simply says one plain text sentence.
{renderRichText(event.description.raw)}
However the call above returns:
My event.description looks like this in the console:
description: raw: "{"data":{},"content":[{"data":{},"content":[{"data":{},"marks":[],"value":"Hard beats. Fast music. Sweaty bodies. ","nodeType":"text"}],"nodeType":"paragraph"}],"nodeType":"document"}"
I can see this is a string and I've tried to play with JSON.parse and JSON.stringify before sending it in. But I can't tell what's the issue. It's a string and it can't read it as a JSON object.** I validated it as proper JSON too.**
event.js
const Event = ({ pageContext }) => {
const { event } = pageContext
console.log('grabbing event', event)
const image = event.flyer
return (
<>
<h2 className={styles.EventTitle}>{event.title}</h2>
<div className={`row`}>
<div className={` ${styles.EventInfo}`}>
{renderRichText(event.description.raw)}
</div>
</div>
<div className={`row`}>
<img className={styles.EventImage} src={image}/>
<div className={` ${styles.EventActions}`}>
<Link to={event.ticket}><button className={styles.EventButton}>Tickets</button></Link>
</div>
</div>
</>
)
};
export default Event;
gatsby-node.js
const path = require("path")
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const queryContentfulResults = await graphql(`
query Events {
allContentfulEvent {
nodes {
contentful_id
title
starttime
id
ad_mats {
file {
url
}
}
square_flyer {
file {
url
}
}
ticketlink
slug
venue {
name
street_address
city
}
description {
raw
}
}
}
}
`)
const eventTemplate = path.resolve(`src/templates/event.js`);
contentfulEvents = queryContentfulResults.data.allContentfulEvent.nodes.map( data => transformEvent(data));
contentfulEvents.forEach(node => {
console.log(node);
createPage({
path: `/events/${node.slug}`,
component: eventTemplate,
context: {
event: node
}
});
});
};
const transformEvent = (data) => {
console.log(data);
return {
title: data.title,
description: data.description,
slug: data.slug,
ad_mats: data.ad_mats,
flyer: data.square_flyer.file.url,
ticket: data.ticketlink,
date: data.starttime,
venue: data.venue.name,
address: data.venue.street_address,
city: data.venue.city
}
};
Mess around with as a string and as JSON before setting it to the function renderRichText,
added an empty options object, and validated the JSON. No changes from this.
答案1
得分: 1
检查包的文档,似乎您将错误的对象传递给 renderRichText()
方法。您不需要传递 event.description.raw
。您应该只传递 event.description
。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论