Javascript SyntaxError:JSON中位于位置0的意外标记
我有一个netlify函数,它返回一个正确的数据以及成功消息,我将其调用到我的api。虽然当我试图在我的react前端显示数据时,它给了我承诺状态为拒绝的错误Javascript SyntaxError:JSON中位于位置0的意外标记,javascript,reactjs,function,lambda,netlify,Javascript,Reactjs,Function,Lambda,Netlify,我有一个netlify函数,它返回一个正确的数据以及成功消息,我将其调用到我的api。虽然当我试图在我的react前端显示数据时,它给了我承诺状态为拒绝的错误 Promise{pending} [[promiseStatus]]: rejected [[PromiseValue]]: SyntaxError: Unexpected token S in JSON at position 0 从graphql读取数据的函数 readWeight.js import fetch from 'nod
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)
}
}
const readAll = () => {
return fetch('/.netlify/functions/readWeight').then((response) => {
console.log(response)
return response.json()
})
}
api.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)
}
}
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
app.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)
}
}
const readAll = () => {
return fetch('/.netlify/functions/readWeight').then((response) => {
console.log(response)
return response.json()
})
}
import React,{Component}来自“React”
从“./api”导入api
导出默认类应用程序扩展组件{
状态={
重量:[]
}
componentDidMount(){
//取回所有待办事项
console.log(api.readAll())
api.readAll().then((t)=>{
这是我的国家({
重量:重量
})
})
}
render(){
返回(
试验
{this.state.todos}
)
}
}
因此,根据您的代码,我们可以假设这是在api.js
文件中注销了一些内容:console.log(response)
?什么是注销?另外,除非我读错了,否则在你的句柄中,你似乎在等待来自GraphQL端点的响应,但你似乎没有在任何地方使用响应?谢谢@TomFinney,我已经更新了代码,它可以工作。因此,根据你的代码,我们可以假设这是在api.js
文件中注销了一些内容:console.log(response)
?什么是注销?另外,除非我读错了,否则在你的句柄中,你似乎在等待来自GraphQL端点的响应,但是你似乎没有在任何地方使用响应?谢谢@TomFinney,我已经更新了代码,它可以工作了。