You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法获取Axios GET响应指定值,打印结果始终为undefined

问题原因及解决办法

从你提供的日志来看,response.data是一个数组(内部包含单个对象元素),而非单独的对象。你直接通过response.data.id或response.data.ID访问属性,本质是在数组对象上查找id/ID属性——但数组本身并不具备这些属性,因此返回undefined。

解决步骤

  1. 先提取数组中的第一个对象元素:response.data[0]
  2. 再根据对象内的实际键名访问对应值,从日志可见对象的键为大写ID,因此正确写法为:
console.log(response.data[0].ID);

修改后的完整代码:

const response = await axios.get(`${apiurl}/get/id/${id}`);
console.log(response.data);
// 先取数组第一个元素,再访问ID属性
console.log(response.data[0].ID);

内容的提问来源于stack exchange,提问作者janGlin4171

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 16:30:45