无法获得{data.allFile.edges.map{node}}来使用Gatsby + GraphQL渲染多个图像 复制步骤预期结果实际结果环境

如何解决无法获得{data.allFile.edges.map{node}}来使用Gatsby + GraphQL渲染多个图像 复制步骤预期结果实际结果环境

说明

我正在尝试使用{data.allFile.edges.map(({ node })将多个(本地)图像渲染到页面上,但是我无法成功运行gatsby develop

复制步骤

我的代码如下:

import React from "react"
import WideGalleryLayout from "../components/wide-gallery-layout"
import Img from "gatsby-image"
import { useStaticQuery,graphql } from "gatsby"
import { css } from "@emotion/core"

export default () => {
  const data = useStaticQuery(graphql`
    query imageQuery {
      allFile(
        filter: {
          extension: { regex: "/(jpg)/" }
          relativeDirectory: { eq: "images/japan" }
        }
      ) {
        edges {
          node {
            base
            childImageSharp {
              fluid {
                aspectRatio
                base64
                src
                srcSet
                sizes
              }
            }
          }
        }
      }
    }
  `)

  return (
    <WideGalleryLayout>
      {data.allFile.edges.map(({ node })
      => (
        <Img fluid=
        {node.childImageSharp.fluid}
        />
      ))}
    </WideGalleryLayout>
  )
}

运行gatsby develop时,出现以下错误消息:

success open and validate gatsby-configs - 0.036s
success load plugins - 1.219s
success onPreInit - 0.003s
success initialize cache - 0.005s
success copy gatsby files - 0.055s
success onPreBootstrap - 0.015s
success createSchemaCustomization - 0.008s
success source and transform nodes - 0.369s
success building schema - 0.241s
success createPages - 0.037s
success createPagesStatefully - 0.075s
success updating schema - 0.022s
success onPreExtractQueries - 0.002s

 ERROR #85911  GRAPHQL

There was a problem parsing "/Users/oliverholms/Dropbox/omh-blog/src/components/japan-component.js"; any GraphQL
fragments or queries in this file were not processed.

This may indicate a syntax error in the code,or it may be a file type
that Gatsby does not know how to parse.

File: src/components/japan-component.js


 ERROR #85911  GRAPHQL

There was a problem parsing "/Users/oliverholms/Dropbox/omh-blog/src/pages/folio-1.js"; any GraphQL
fragments or queries in this file were not processed.

This may indicate a syntax error in the code,or it may be a file type
that Gatsby does not know how to parse.

File: src/pages/folio-1.js

failed extract queries from components - 0.314s
success write out redirect data - 0.010s
success onPostBootstrap - 0.002s
info bootstrap finished - 5.465s
success run page queries - 0.021s - 4/4 189.04/s
success write out requires - 0.004s
warn ESLintError:
/Users/oliverholms/Dropbox/omh-blog/src/components/layout.js
  3:26  warning  'Link' is defined but never used           no-unused-vars
  8:9   warning  'data' is assigned a value but never used  no-unused-vars

✖ 2 problems (0 errors,2 warnings)

warn ESLintError:
/Users/oliverholms/Dropbox/omh-blog/src/pages/folio-2.js
  3:10  warning  'Image' is defined but never used  no-unused-vars

✖ 1 problem (0 errors,1 warning)

warn ESLintError:
/Users/oliverholms/Dropbox/omh-blog/src/pages/folio-3.js
  3:10  warning  'Image' is defined but never used  no-unused-vars

✖ 1 problem (0 errors,1 warning)


 ERROR #98123  WEBPACK

Generating development JavaScript bundle failed


/Users/oliverholms/Dropbox/omh-blog/src/pages/folio-1.js
  39:7  error  Parsing error: Unexpected token,expected ","

  37 |     <WideGalleryLayout>
  38 |       {data.allFile.edges.map(({ node })
> 39 |       => (
     |       ^
  40 |         <Img fluid=
  41 |         {node.childImageSharp.fluid}
  42 |         />

✖ 1 problem (1 error,0 warnings)


File: src/pages/folio-1.js


 ERROR #98123  WEBPACK

Generating development JavaScript bundle failed

/Users/oliverholms/Dropbox/omh-blog/src/pages/folio-1.js: Unexpected token," (39:6)

  37 |     <WideGalleryLayout>
  38 |       {data.allFile.edges.map(({ node })
> 39 |       => (
     |       ^
  40 |         <Img fluid=
  41 |         {node.childImageSharp.fluid}
  42 |         />

File: src/pages/folio-1.js:39:6

failed Building development bundle - 3.355s
warn ESLintError:
/Users/oliverholms/Dropbox/omh-blog/src/components/layout.js
  3:26  warning  'Link' is defined but never used           no-unused-vars
  8:9   warning  'data' is assigned a value but never used  no-unused-vars

✖ 2 problems (0 errors," (39:6)

  37 |     <WideGalleryLayout>
  38 |       {data.allFile.edges.map(({ node })
> 39 |       => (
     |       ^
  40 |         <Img fluid=
  41 |         {node.childImageSharp.fluid}
  42 |         />

File: src/pages/folio-1.js:39:6

failed Re-building development bundle - 0.083s
 ERROR #98123  WEBPACK

我正在按照这个笨蛋视频Add multiple images from a directory with gatsby-image中的步骤进行操作。我有99%的把握可以正确复制他的代码-使用文件的正确目录,但是CLI无法成功develop

预期结果

我应该能够使用gatsby-image从目录中渲染多个图像。我已经在http:// localhost:8000 / ___ graphql处进行了检查,并且我的graphQL查询返回了我的图像以及我请求的其他数据。

实际结果

我无法成功gatsby develop或渲染图像。终端错误消息指出箭头功能是问题元素(“预期逗号”等)。

我对此并不陌生,因此请在您的回答中将此列为技术语言。

环境

  System:
    OS: macOS 10.15.5
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.18.1 - /usr/local/bin/node
    npm: 6.14.5 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 84.0.4147.125
    Safari: 13.1.1
  npmPackages:
    gatsby: ^2.23.12 => 2.23.12
    gatsby-image: ^2.4.16 => 2.4.16
    gatsby-plugin-emotion: ^4.3.10 => 4.3.10
    gatsby-plugin-sharp: ^2.6.27 => 2.6.27
    gatsby-plugin-typography: ^2.5.10 => 2.5.10
    gatsby-source-filesystem: ^2.3.23 => 2.3.23
    gatsby-transformer-json: ^2.4.11 => 2.4.11
    gatsby-transformer-remark: ^2.8.27 => 2.8.27
    gatsby-transformer-sharp: ^2.5.13 => 2.5.13
  npmGlobalPackages:
    gatsby-cli: 2.12.87

解决方法

我建议您尝试像下面这样编码

{data.launches &&
    data.launches.launches &&
    data.launches.launches.map((launch: any) => (
      <LaunchTile key={launch.id} launch={launch} />
    ))}

参考网址:https://www.apollographql.com/docs/tutorial/queries/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-