“Gatsby Contentful 富文本原始内容未定义”

huangapple go评论73阅读模式
英文:

gatsby contentful rich text raw is undefined

问题

在Gatsby中尝试创建一个简单的网站。在我的特定示例中,我有一个带有来自Contentful的富文本字段的节点。它只是一个普通的文本句子。

{renderRichText(event.description.raw)}

然而,上面的调用返回:

“Gatsby Contentful 富文本原始内容未定义”

在控制台中,我的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:

“Gatsby Contentful 富文本原始内容未定义”

My event.description looks like this in the console:

description: raw: &quot;{&quot;data&quot;:{},&quot;content&quot;:[{&quot;data&quot;:{},&quot;content&quot;:[{&quot;data&quot;:{},&quot;marks&quot;:[],&quot;value&quot;:&quot;Hard beats. Fast music. Sweaty bodies. &quot;,&quot;nodeType&quot;:&quot;text&quot;}],&quot;nodeType&quot;:&quot;paragraph&quot;}],&quot;nodeType&quot;:&quot;document&quot;}&quot;

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 }) =&gt; {
const { event } = pageContext
console.log(&#39;grabbing event&#39;, event)
const image = event.flyer
return (
&lt;&gt;
&lt;h2 className={styles.EventTitle}&gt;{event.title}&lt;/h2&gt;
&lt;div className={`row`}&gt;
&lt;div className={` ${styles.EventInfo}`}&gt; 
{renderRichText(event.description.raw)}
&lt;/div&gt;
&lt;/div&gt;
&lt;div className={`row`}&gt;
&lt;img className={styles.EventImage} src={image}/&gt;
&lt;div className={` ${styles.EventActions}`}&gt;
&lt;Link to={event.ticket}&gt;&lt;button className={styles.EventButton}&gt;Tickets&lt;/button&gt;&lt;/Link&gt; 
&lt;/div&gt;
&lt;/div&gt;
&lt;/&gt;
)
};
export default Event;

gatsby-node.js

const path = require(&quot;path&quot;)
exports.createPages = async ({ graphql, actions }) =&gt; {
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 =&gt; transformEvent(data));
contentfulEvents.forEach(node =&gt; {
console.log(node);
createPage({
path: `/events/${node.slug}`,
component: eventTemplate,
context: {
event: node
}
});
});
};
const transformEvent = (data) =&gt; {
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

您可以在这里这里找到更多信息。

英文:

Checking the documentation of the package, it seems like you're passing the wrong object to the renderRichText() method. You don't need to pass event.description.raw. You should only pass event.description.

You can find more information here and here.

huangapple
  • 本文由 发表于 2023年7月14日 08:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定