javascript - NodeJS Axios 响应在 console.log API 上未定义

标签 javascript node.js api axios http-get

我尝试让这段代码工作。

const axios = require('axios');
let bodyapi = axios.get('there's my api')
console.log(bodyapi.data) <- undefined
let body = bodyapi.data
console.log(body.discord) <- couldn't get parameter ''discord'' of undefined

API的响应类型:

"discord":{"Category":"activation","Qty":1542,"Price":1}
"vkontakte":{"Category":"activation","Qty":133,"Price":21}

我得到它“未定义”。在 NodeJS 上运行。

最佳答案

axios 返回 promise

const axios = require('axios');

// use async await 
(async ()=>{
let bodyapi = await axios.get('there's my api')
console.log(bodyapi.data) // response
})()

// other way
axios.get('there's my api').then(data=> console.log(data))

关于javascript - NodeJS Axios 响应在 console.log API 上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59963550/

相关文章:

javascript - 如何在删除和添加类后重新启用点击功能?

node.js - 使用 sequelize 将数据库前缀附加到表名

javascript - 为什么 Node.js HTTP 服务器不响应来自 Python 的请求?

javascript - node.js oauth2 服务器到服务器客户端(谷歌跟踪身份验证)

javascript - angularjs foreach循环通过依赖于先前请求的http请求

javascript - 在 heroku 上 headless 执行 chrome 时出错

android - 使用 'gradlew assembleRelease' react 原生 Android 构建失败

api - 如何将图片发布到 Instagram?

javascript - 无法使用 Promise 和 AJAX 请求获取 API 显示 Json 数据

javascript - 如何在声明 WebSocket HTML5 时发送参数