【发布时间】:2020-12-03 17:01:13
【问题描述】:
JSON 响应如图 1 所示。
我能够使用 axios(已经使用 JSON.parse)将整个响应分配给状态(命名配置文件)。
当profile.bio 和profile.image 工作时;profile.user.username 等不工作并抛出错误 - Undefined is not an object
const [profile, setProfile] = useState({});
const phone_no = phone.phone;
const fetchProfile = useEffect(() => {
var res = {};
axios
.get('<API URL>' + phone_no)
.then((response) => (res = response.data))
.then(() => {
setProfile(res);
})
.then(() => console.log(profile))
.catch((e) => console.log(e)); });
const user_stream = {
name: first.first_name,
image: profile.image,
id: profile.user.id,
};
更新 - 解决方案:将 async-await 与 axios 一起使用,已修复。
【问题讨论】:
-
使用您发布的图像上的数据,它应该可以工作...或发布您的代码以检查是否存在错误
-
请将代码示例放在帖子正文中,不要作为图片链接。
-
添加的代码,见 user_stream - image: profile.image 正在工作;但不是 id:profile.user.id
-
这个问题(现在?)令人困惑,因为它引用了一个 image 1,但没有。
标签: javascript json reactjs react-native axios