无法从JS客户端中的REST API提取调用中获取数据

时间:2018-11-30 02:01:30

标签: node.js rest express

因此,我的客户端javascript中包含以下代码:

async function getTotals() {
    var response = await fetch('/totals')
    console.dir(response.json())
    console.log("Update totals!")
}

在我的nodejs express应用中,GET如下:

app.get('/totals', async function(req, res){
    res.json({
        totals: 100,
        allowance: 50
    });
});

如果我用浏览器到达终点,我会看到响应很好,但是在我的客户端网页中根本没有任何正文。

我想念我的世界是什么?

1 个答案:

答案 0 :(得分:1)

response.json()返回一个承诺。您需要使用await

对其进行调用
async function getTotals() {
    var response = await fetch('/totals');
    console.dir(await response.json());
    console.log("Update totals!", json);
}