gpt4 book ai didi

javascript - 无法访问对象属性,即使我可以看到它们存在

转载 作者:行者123 更新时间:2023-11-28 17:00:56 25 4
gpt4 key购买 nike

我无法访问任何对象属性,一直处于未定义状态。

我已经尝试过

console.log(JSON.parse(this.$store.state.user.userId));

console.log(JSON.parse(this.$store.state.user[0].userId));

当我这样做的时候

console.log(JSON.parse(this.$store.state.user.userId));

我明白

"SyntaxError: Unexpected token u in JSON at position 0"

当我这样做的时候

 console.log(JSON.parse(this.$store.state.user));

我得到了对象并且可以看到属性。只是每当我尝试访问它们时,我都会得到未定义

最佳答案

When I just do console.log(JSON.parse(this.$store.state.user)); I get the object and I can see the properties.

这意味着 this.$store.state.user 包含描述 user 对象的 JSON 字符串。

因此 JSON.parse(this.$store.state.user.userId) 不正确。在这种情况下,您尝试从字符串获取属性 userId ,获取 undefinedJSON.parse 函数在第一个符号上失败,即 “u”

您应该使用 JSON.parse(this.$store.state.user).userId 来代替。

关于javascript - 无法访问对象属性,即使我可以看到它们存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516671/

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