graphql - 使用 gatsby-image 和 graphql 获取图像时不断出错
问题描述
我正在尝试添加来自 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>
)
}
解决方案
我不需要 ChildImageSharp 部分,我认为这仅用于查询文件系统。
推荐阅读
- node.js - 在 OAuth2 身份验证调用和重定向调用之间传递值
- swift - 从 SVG HTML 字符串创建 UIImage
- powershell - PowershellCore - 无一例外地获取非 20 倍响应的内容
- javascript - 来自 Photoshop 的通道图像数据中的 Zip 压缩
- flutter - Flutter GraphQL FormatException, FormatException: Unexpected character (at character 1
- c# - C# 将一个逗号分隔的字符串转置到另一个上
- gatsby - 如何自定义图片的publicURL
- c# - 如何在 Visual Studio 中将 WInform 连接到 Webform
- c# - 如何修改另一个 ASPX 文件
- javascript - 如何在 R 中使用传单制作的地图上添加下载按钮?