【问题标题】:Key value is missing when I use axios使用 axios 时缺少键值
【发布时间】:2019-06-21 19:14:28
【问题描述】:

当我使用 axios 拉入 JSON 数据时,它不会从 JSON 中引入初始密钥。

如果我导入酒单(JSON 文件)并 console.log 响应,它会显示“wines:”,然后是葡萄酒数组,这就是我想要的,但我需要使用 axios。当我使用 axios 获取 JSON 文件时,它只显示数组中的项目数,然后显示葡萄酒数组。 axios 有没有办法引入关键价值“葡萄酒”?

我试图与其余数据一起获取的 JSON 密钥是“wines”:

{ "wines": [{ "id": "f2ca57a5-d9da-4808-9164-8d6e0da0aef5",...

这是我的 axios 调用:

const winesInfo = axios.get('/api/v1/wines').then(function(response) { console.log(response.data.wines); }) .catch(function(error) { console.log(error); });

我要console.log:

{wines: Array(12)}

我的 console.log 是什么样子的:

(12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

【问题讨论】:

  • console.log(response.data);?
  • 谢谢埃德加!

标签: json reactjs axios


【解决方案1】:

您需要访问 response.data 以获取关键葡萄酒,但您正在访问 response.data.wines,因此显然您将获得没有关键葡萄酒的对象数组

所以,response.data 会给你你想要的

改变

  const winesInfo = axios.get('/api/v1/wines').then(function(response) { console.log(response.data.wines); }).catch(function(error) { console.log(error); });

  const winesInfo = axios.get('/api/v1/wines').then(response => { console.log(response.data); }).catch(error => { console.log(error); });

【讨论】:

  • 谢谢赫马德里!
猜你喜欢
  • 2022-06-30
  • 2021-08-16
  • 2021-06-11
  • 1970-01-01
  • 2021-02-16
  • 2018-10-13
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多