反应读取数据形成服务器
当我将特定数据获取到前端React应用程序中时,我会从Strapi后端数据中遇到错误,当我将数据记录到浏览器控制台中时,我会发现错误,我会在浏览器控制台中看到我的数据。
query GetPromoProducts {
allStrapiProduct(filter: {promo: {eq: true}}) {
edges {
node {
strapiId
name
variants {
images {
url
}
}
}
}
}
}
`)
const [selectedSlides, setSelectedSlides] = useState(0)
console.log(data);
var slides = [];
data.allStrapiProduct.edges.map(({node}) => {
console.log(node.variants[0].images[0]);
})
I am getting an error from strapi backend data when I fetch particular data into the front end react app I get an error when I log the data into the browser console I see my data in the browser console.
query GetPromoProducts {
allStrapiProduct(filter: {promo: {eq: true}}) {
edges {
node {
strapiId
name
variants {
images {
url
}
}
}
}
}
}
`)
const [selectedSlides, setSelectedSlides] = useState(0)
console.log(data);
var slides = [];
data.allStrapiProduct.edges.map(({node}) => {
console.log(node.variants[0].images[0]);
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调试
要调试服务器端,使用和
console.log.log
< /a>要努力到返回null的对象。基于问题,不确定确切的代码是什么样的。
要停止在这一区域发生:
可能会键入与GraphQl GraphQl结构化数据的弹性反应
,但是除非需要字段,否则您必须处理
null
。要使客户端代码更加健壮,您可以使用
array.prototype.filter()
跳过没有图像的对象,或 - 如此示例中,使组件处理丢失数据的情况。这完全取决于您希望前端应用程序渲染它。我尚未测试此代码,因此将其作为“ pseudo-jsx”来演示处理空的GraphQl响应字段。
Debugging
To debug the server side, use optional chaining and
console.log
to work your way up to the object's that returning null.Not sure what the exact code looks like, based on question.
To stop right at the area this happens:
Resilient React with GraphQL
GraphQL structured data may be typed, but unless the fields are required, you have to handle
null
.To make the client-side code more robust, you can use
Array.prototype.filter()
to skip objects with no images, or - as in this example - make components handle the case where data is missing. It's entirely up to how you want the frontend app to render it.I haven't tested this code, so take it as "pseudo-JSX" to demonstrate handling empty graphql response fields.