未处理的承诺拒绝TypeError

时间:2019-06-04 11:17:18

标签: javascript node.js reactjs async-await

我正在使用async / await对数据库进行查询并获取结果,但是我不知道为什么在浏览器控制台中会出现此错误:

  

未处理的承诺拒绝TypeError:“游戏未定义”

我有两个函数,agetData是异步的,并调用getGames:

row["t_in_o"].ToString("yyyy-MM-dd HH:mm:ss.fff");

在浏览器控制台中,我得到了以下结果:

enter image description here

为什么不在这里异步/等待?我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您刚刚忘记了return

getGames(){
    let query = HOMELF1_NEXT_GAMES;
    var data = {
        "query": query,
        "variables": null
    }
     \/ here
    return fetch(URL_FEB_API, {
        method: "POST",
        headers: {
            "Accept"        :   "application/json",
            "content-type"  :   "application/json"
        },
        body: JSON.stringify(data)
    })
    .then(response => {
        return response.json()
    })
    .then(data => {
        console.log("End query");
        return data;
    })
    .catch(err => {
        console.log("Error: " + err)
    })
}

async getData(){
    console.log("Before getGames");
    let games = await this.getGames();
    console.log("After getGames");
    console.log("games: " + games.length)
}

componentDidMount(){
    this.getData();
};