【问题标题】:I keep getting errors when getting image with gatsby-image and graphql使用 gatsby-image 和 graphql 获取图像时不断出错
【发布时间】:2020-03-09 11:02:33
【问题描述】:

我正在尝试添加来自 graphql 的图像。我现在已经遇到过几次这个问题,并且总是幸运地解决了这个问题。

查询图像时,我得到以下响应:

“路径”参数必须是字符串、缓冲区或 URL 类型之一。 接收类型未定义

代码如下:

import React from 'react'
import { Link, graphql, useStaticQuery, StaticQuery } from 'gatsby'
import Img from 'gatsby-image'
import './longCard.css';

const CardData = props => {

    const slug = props.slug;
    return (
        <StaticQuery
            query={
                graphql`
            query($slug: String) {
                sanityProduct(slug: {current: {eq: $slug}}) {
                    slug{ 
                        current
                    }
                    title
                    featured_image {
                        asset {
                            childImageSharp {
                                fixed {
                                    ...GatsbyImageSharpFixed
                                }
                            }
                        }
                    }
                }
            }
`}
            render={data => <LongCard />}
        />
    )
}

export default CardData

export const LongCard = ({ data }) => {
    return (
        <div className="long-card">
            <div className="long-card-inner">
                <Link to={data.sanityProduct.slug.current}>{data.sanityProduct.title}</Link>
                {/* Add image */}
                <Img fixed={data.featured_image.asset.childImageSharp.fixed} />
            </div>
        </div>

    )
}

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    我不需要 ChildImageSharp 部分,我认为这仅用于查询文件系统。

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 2021-03-29
      • 2021-12-31
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多