【问题标题】:How to get an item from object in AsyncStorage saved?如何从保存的 AsyncStorage 中的对象中获取项目?
【发布时间】:2019-10-20 07:48:40
【问题描述】:

我保存了一个对象,其中包含电子邮件、用户名等用户数据。

我使用 AsyncStorage 来保存它们, 当我得到它们时,当我登录或在我的 textInput 中使用它时,我只会看到一个字符串 那么如何处理这个问题并只获取我保存的 Just Email 或 Just Username 等特定数据?

我的代码

注册

 const {
      username,
      email,
      city,
      mobileNumber,
    } = this.state;

  const profileData = {
        "username": username,
        "email": email,
        "city": city,
        "mobileNumber": mobileNumber
      }
 firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(async user => {
          firebase
            .database()
            .ref(`users/${user.user.uid}`)
            .set({
              username: username,
              type: type,
              email: email,
              city: city,
              mobileNumber: mobileNumber,
              token: fcmToken
            });
          this.props.navigation.navigate("Home");
          await AsyncStorage.setItem('@MyProfile:data', JSON.stringify(profileData)).then(() => console.log("Saved"));
        }).catch(err => console.log(err));

个人资料屏幕

async componentDidMount() {
        try {
            await AsyncStorage.getItem('@MyProfile:data')
                .then(data => this.setState({ data }))
                .catch(error => console.log('@error' + error));

                console.log(this.state.data); // 
        } catch (error) {
            console.log("@CError", error);
        }
    }

render(){
return(
   <View style={styles.logoSection}>
                                {/* <SvgComponent height={100} /> */}
                                <Icon name="ios-contact" size={90} color='#4d8dd6' style={{ marginTop: 9 }} />

                                <Text style={{ fontSize: 18, color: "#000", margin: 35, marginTop: 7 }}>{this.state.data}</Text> // i wnat just display username here from storage
                            </View>
)
}

控制台

  console.log(this.state.data); 

我认为作为一个字符串:{"username":"user","email":"user@user.us","city":"usercity","mobileNumber":"0597979979"}

【问题讨论】:

  • 我没有发现问题?只需 JSON.parse(this.state.data) 即可返回对象。
  • @basic 请将其添加为答案
  • Parse JSON in JavaScript?的可能重复
  • @JuniusL。我宁愿把它当作一个骗子来关闭它,而不是为同样的旧事物添加一个答案。
  • 好的,但是当我在 中显示这些 JSON.parse(this.state.data.username) 时,我收到一个错误“无法读取未定义的属性'用户名'”

标签: javascript reactjs react-native async-await react-redux


【解决方案1】:

{this.state.data.username}{this.state.data['username']}

但可能在 {this.state.data} 内的这一行中,您在第一次渲染时将没有用户名访问权限,可能 this.state.data 将为空,因此您需要在使用之前进行验证this.state.data['username']

【讨论】:

  • 我在用户注册时验证它们我将它们保存到存储中,现在我在用户注销时遇到一些问题我在注销功能中使用 asyncStorage.clear(),现在当他登录时再次访问他的个人资料,他会因为存储空间为空而出现错误,并且在他注册时存储步骤起作用,那么如何处理呢?
  • 发生什么错误?如果是因为你没有用户名的访问权限,你需要在访问de prop之前检查!比如 if(this.state.data) this.state.data['username']
  • 错误,是因为退出时删除了存储,我不明白th​​is.state.data['username']是什么意思,为什么我这样做以及在哪里?
  • TypeError: TypeError: Cannot read property 'username' of null
  • 我知道是因为用户名是空的,因为我清空了存储,先检查一下likeif(this.state.data){ .. put my elements .. } else { // what i should do here? },我认为这对我们的用户不友好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
相关资源
最近更新 更多