首页 > 解决方案 > 无法读取未定义的属性“映射”-当我尝试在 Gatsby 项目的构建时从 api 端点获取数据时出错

问题描述

gatsby-source-apiserver是一个 gatsby 源插件,用于提取第三方 api 数据。

当我使用两个gatsby-source-apiserver插件从不同的 url 获取数据时,它可以工作。但是当我添加第三个时它不起作用

我尝试了第三个本身,但使用gatsby build.

错误:

gatsby build
success open and validate gatsby-configs - 0.225 s
success load plugins - 35.556 s
success onPreInit - 0.005 s
success delete html and css files from previous builds - 0.142 s
success initialize cache - 0.309 s
success copy gatsby files - 2.744 s
success onPreBootstrap - 0.058 s

 ERROR #11321  PLUGIN

"gatsby-source-apiserver" threw an error while running the sourceNodes lifecycle:

Cannot read property 'map' of undefined



  TypeError: Cannot read property 'map' of undefined

  - normalize.js:138 standardizeKeys
    [xxx-xxx]/[gatsby-source-apiserver]/normalize.js:138:46

  - normalize.js:29 Object.exports.createNodesFromEntities
    [xxx-xxx]/[gatsby-source-apiserver]/normalize.js:29:14

  - gatsby-node.js:115 
    [xxx-xxx]/[gatsby-source-apiserver]/gatsby-node.js:115:19

  - Generator.next

  - gatsby-node.js:1 step
    [xxx-xxx]/[gatsby-source-apiserver]/gatsby-node.js:1:253

  - gatsby-node.js:1 
    [xxx-xxx]/[gatsby-source-apiserver]/gatsby-node.js:1:423

  - next_tick.js:68 process._tickCallback
    internal/process/next_tick.js:68:7


⠇ source and transform nodes
{
      resolve: 'gatsby-source-apiserver', // REF: https://www.gatsbyjs.org/packages/gatsby-source-apiserver/
      options: {
        // Type prefix of entities from server
        typePrefix: 'internal__',

        // The url, this should be the endpoint you are attempting to pull data from
        url: GET_DATA_URL,

        method: 'get',

        // Name of the data to be downloaded.  Will show in graphQL or be saved to a file
        // using this name. i.e. posts.json
        name: `data`,

        // Nested level of entities in response object, example: `data.posts`
        entityLevel: `data`,

        schemaType: dataSchema,

        // Optionally save the JSON data to a file locally
        // Default is false
        localSave: false,

        //  Required folder path where the data should be saved if using localSave option
        //  This folder must already exist
        path: `${__dirname}/src/assets/data/`,

        // Optionally include some output when building
        // Default is false
        verboseOutput: true, // For debugging purposes

        // Optionally skip creating nodes in graphQL.  Use this if you only want
        // The data to be saved locally
        // Default is false
        skipCreateNode: false, // skip import to graphQL, only use if localSave is all you want
        entitiesArray: [{
          url: GET_DATA_URL,
          method: 'get',
          headers: {
            'Content-Type': 'application/json'
          },
          name: `data`,
        }]
      }
    },

它应该可以正常工作。我正在使用的软件包:

    ...
    "gatsby-source-apiserver": "^2.1.3",
    "gatsby": "^2.3.34",

标签: gatsby

解决方案


推荐阅读