javascript - SyntaxError:JSON 中位置 0 的意外标记 S
问题描述
我有一个 netlify 函数,它返回正确的数据以及我调用我的 api 的成功消息。虽然当我试图在我的反应前端显示数据时,它给了我一个错误,承诺状态被拒绝。
Promise{pending}
[[promiseStatus]]: rejected
[[PromiseValue]]: SyntaxError: Unexpected token S in JSON at position 0
从 graphql readWeight.js读取数据的函数
import fetch from 'node-fetch'
exports.handler = async() => {
console.log("inside");
const response = await fetch(
'https://graphql.fauna.com/graphql',
{
method: 'POST',
headers: {
Authorization: `Bearer ${API_SECRET}`
},
body: JSON.stringify({
query:`
{allweight{data{weight}}}
`
})
})
.then(res => res.json())
.catch(err => console.log(err))
//console.log(response.data.allweight.data.map(w=>console.log(w.weight)))
//console.log(response)
return {
statusCode: 200,
body: JSON.stringify(response)
}
}
api.js
const readAll = () => {
return fetch('/.netlify/functions/readWeight').then((response) => {
console.log(response)
return response.json()
})
}
api.js 响应
Response {type: "basic", url: "http://localhost:3000/.netlify/functions/readWeight", redirected: false, status: 200, ok: true, …}
type: "basic"
url: "http://localhost:3000/.netlify/functions/readWeight"
redirected: false
status: 200
ok: true
statusText: "OK"
headers: Headers {}
body: (...)
bodyUsed: true
__proto__: Response
应用程序.js
import React, { Component } from 'react'
import api from './api'
export default class App extends Component {
state = {
weight: []
}
componentDidMount() {
// Fetch all todos
console.log(api.readAll())
api.readAll().then((t) => {
this.setState({
weight: weight
})
})
}
render(){
return(
<h1>Test
{this.state.todos}</h1>
)
}
}
解决方案
推荐阅读
- java - 线程“JavaFX 应用程序线程”中的异常 java.lang.NoClassDefFoundError
- laravel-5.7 - 从 Laravel 中的多个表中获取数据
- python - 音频处理中 str.replace 的模拟
- sql-server-2012 - 动态 sql 中的语法不正确
- javascript - 为什么我的脚本在添加 d3plus.js 图表时没有加载?
- reactjs - create-react-app webapp - ReferenceError: Can't find variable 'Map' - on iOS 7/Safari
- android - Ionic 4 提交后不隐藏键盘
- sql - 进行内部联接时,索引顺序很慢
- javascript - 如何让用户编辑上下文菜单中的自定义按钮重定向到的 URL?
- node.js - 在 node.js 搜索到 mongodb 期间无法发布错误