未捕获(承诺)SyntaxError:意外令牌?

时间:2019-11-08 09:49:52

标签: javascript reactjs

我有这样的错误:

  

index.js:8未捕获(承诺)语法错误:意外令牌<中   JSON位于位置0

index.js(操作):

import { days } from "../components/app";
export function fetchData (day) {
    return async (dispatch) => {dispatch({ type: 'LOAD_DATA_START', day });
    const response = await fetch(`api.iev.aero/api/flights/${day}`); 
    const data = (await response.json()).body; 
    dispatch({ type: 'LOAD_DATA_END', data });
    dispatch({ type: 'SET_SHIFT', data }); 
    } 
}

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

您应该取下尸体。

const data = (await response.json()); 

这是我通常要做的:

const callAPI = () => new Promise((resolve, reject) => 
fetch(url, options)
.then(res => res.json())
.then(res => resolve(res))
.catch(err => reject(err))
)

import { days } from "../components/app";
export function fetchData (day) {
    return async (dispatch) => {
    dispatch({ type: 'LOAD_DATA_START', day });
    const response = await fetch(`api.iev.aero/api/flights/${day}`); 
    const data = await response.json(); 
    dispatch({ type: 'LOAD_DATA_END', data });
    dispatch({ type: 'SET_SHIFT', data }); 
    } 
}

如果仍然出现错误,请打开检查窗口,转到“网络”选项卡并共享​​该API调用的屏幕截图。