简体   繁体   中英

GATSBY: TypeError: Cannot read property 'includes' of undefined

I'm having some trouble filtering out images in my portfolio site laid out in this post…

This is the error I keep getting: "TypeError: Cannot read property 'includes' of undefined"

Anyone know why this might be?

Images query:

query {
  images: allFile (
    filter: { ext: { eq: ".jpg" } },
    sort: { order: ASC, fields: [relativePath] }
  )
  {
        edges {
        node {
        fields {
            imgslug
        }
        relativeDirectory
        id
        childImageSharp {
          fixed(width: 100) {
            src
          }
        }
        }
    } 
  }
}

Image display script:

{data.images.edges.filter((node) => node.relativeDirectory.includes(data.images.edges.node.fields.imgslug)).map(image => {
   return (
      <Img key={image.id} fixed={image.childImageSharp.fixed}/>
   )
})}

includes loop returns a boolean, true or false depending on the condition. You may want to use something like:

{data.images.edges.map((node) => {
   if(node.relativeDirectory.includes(data.images.edges.node.fields.imgslug){
     return <Img key={image.id} fixed={image.childImageSharp.fixed}/>
   }
})}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM