【发布时间】:2021-07-30 03:29:08
【问题描述】:
我有一个 gatsby 项目,我需要在其中发出 API 请求以获取汽车列表,我将使用该列表为每辆汽车动态创建页面。 API 响应数据如下:
{
"Fleet_Data": [
{
"Make": "Honda",
"Model": "Pilot"
},
{
"Make": "Honda",
"Model": "CRV"
},
{
"Make": "Honda",
"Model": "Accord"
}
]
}
我正在使用gatsby-source-custom-api 插件。这是我的gatsby-config.js 文件:
{
resolve: "gatsby-source-custom-api",
options: {
url: "https://api.fleetdata.com/",
headers: {
Authorization: 'Basic API_KEY'
},
rootKey: "Fleet_Data",
schemas: {
Fleet_Data: `
Make: String
Model: String
`
}
}
}
我的节点文件中除了调试之外没有任何内容,但这是我的gatsby-node.js
const path = require("path");
exports.createPages = async ({ graphql }) => {
console.log("IT WORKED")
};
但是当我运行 gatsby build 时,我在控制台中收到以下错误:
"gatsby-source-custom-api" threw an error while running the sourceNodes lifecycle:
invalid json response body at https://api.fleetdata.com/ reason: Unexpected token : in JSON at position 4
23 |
24 | const URL = getUrl(process.env.NODE_ENV, url)
> 25 | const data = await fetch(URL).then(res => res.json())
| ^
26 |
27 | const typeDefs = getTypeDefs(schemas, imageKeys)
28 | createTypes(typeDefs)
File: node_modules/gatsby-source-custom-api/gatsby-node.js:25:16
FetchError: invalid json response body at https://api.fleetdata.com/ reason: Unexpected token : in JSON at position 4
- index.js:272
[ProjectCars]/[node-fetch]/lib/index.js:272:32
- task_queues.js:95 processTicksAndRejections
internal/process/task_queues.js:95:5
- gatsby-node.js:25 Object.exports.sourceNodes
[ProjectCars]/[gatsby-source-custom-api]/gatsby-node.js:25:16
- api-runner-node.js:429 runAPI
[ProjectCars]/[gatsby]/src/utils/api-runner-node.js:429:16
not finished source and transform nodes - 3.913s
我在这里做错了什么?
【问题讨论】:
标签: javascript node.js reactjs gatsby gatsby-plugin