SyntaxError: неожиданный токен S в JSON в позиции 0 - PullRequest
1 голос
/ 15 апреля 2020

У меня есть функция 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>
)
}


}
...