Reputation: 163
EDIT: i added the loop code.
Other variables are working fine, so the connection between Contentful and the data layer of my app is not the problem.
I can see the image asset variable including it's src in GraphiQL tool, but in my code it seems to be null. The way i understand GraphiQL, is that it queries a local data layer, so the variables are coming from Contentful just fine. This is a simplified query that i use.
This CardMedia component is in a mapping function, but the JS in that cannot be the problem because everything else works just fine, except the images.
const posts = get(this, 'props.data.allContentfulBlog.edges')
{posts.map(({ node }) => {
return (
<Grid key={node.slug}>
<Card>
<CardMedia
image={node.mainImg.responsiveResolution.src}
title={node.mainTitle}
/>
<CardContent>
<Typography variant="headline" component="div">
{node.mainTitle}
</Typography>
<Typography component="div">
<p
dangerouslySetInnerHTML={{
__html: node.mainText.childMarkdownRemark.excerpt,
}}
/>
</Typography>
</CardContent>
<CardActions>
<Button href={node.slug}>
<span>Read more</span>
</Button>
</CardActions>
</Card>
</Grid>
)
})}
Upvotes: 0
Views: 580
Reputation: 2938
From my understanding to your GQL app, this will be the key to your edges array, which I am supposing you are using it as posts
.
const posts = allContentfulBlog.edges
or maybe const posts = this.props.allContentfulBlog.edges
Thus: we will be using that to loop on our objects inside the posts array.
posts.map((post) => {
return /* Grid */
})
if you have this already, can you please provide a code snapshot to your component where you are performing this loop.
UPDATED:
looking at the results image, there seems like one of the nodes does not have a mainImg
. thus, we will use a default image link in this case.
in your render()
method add this:
const defaultImage =
linkToYourDefaultImage;
in your loop:
{posts.map(({ node }) => {
return (
<Grid key={node.slug}>
<Card>
<CardMedia
image={node.mainImg ? node.main.responsiveResolution.src : defaultImage}
title={node.mainTitle}
/>
......the rest of your code.............
</Grid>
)
})}
changes has been made to CardMedia
component, in image
prop, we have examined whether the value of node.mainImg
if null or not, and return the proper value such like !null ? image : defaultImage
Upvotes: 1