gpt4 book ai didi

javascript - 我无法返回 axios 请求的响应

转载 作者:行者123 更新时间:2023-11-30 23:58:56 24 4
gpt4 key购买 nike

我正在尝试在 Node 中创建一个模块,以使用 axios 请求从 API 返回 json。但是当我尝试从函数 getJson() 返回响应时,什么也没有返回给我。

const axios = require('axios');
const authentication = require('./authentication');


const url = `https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=${authentication.token}`;

const getJson = async () => {
const response = await axios.get(url);
// Here i can see the json normally
console.log(response.data)
return response.data
}

const response = getJson()
// but here nothing is shows to me in console.
console.log(response)

return of console

最佳答案

getJson() 实际上返回 promise实例。您只需要等待,例如:

(async () => {
const response = await getJson()
console.log(response)
})();

关于javascript - 我无法返回 axios 请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60873200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com