反应本机 firebase 检查用户是否已存在于实时数据库中

2024-03-25

我需要检查用户名是否已存在于实时数据库中,然后提示用户选择另一个用户名。一直说没找到。我认为这是因为我的数据是如何嵌套的。

注册.js

const { email, username, password } = this.state;

    await firebase
                .auth()
                .createUserWithEmailAndPassword(email, password)
                .then(async user => {
                    console.log('Data created', user);
                    let rootRef = firebase.database().ref()

                rootRef.child("users")
                        .orderByChild("username")
                        .equalTo(username)
                        .once("value")
                        .then(snapshot => {
                            if (snapshot.exists()) {
                                    let userData = snapshot.val()
                                console.log(userData)
                              Alert.alert('username is taken')
                                return userData;
                            }else {
                                console.log('not found')

                            }
                    })

您正在创建一个用户,然后检查该用户是否存在。创建用户前检查用户是否存在。

const { email, username, password } = this.state;

let rootRef = firebase.database().ref();

rootRef
  .child('users')
  .orderByChild('username')
  .equalTo(username)
  .once('value')
  .then(snapshot => {
    if (snapshot.exists()) {
      let userData = snapshot.val();
      console.log(userData);
      Alert.alert('username is taken');
      return userData;
    } else {
      console.log('not found');
      firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(async user => {
          console.log('Data created', user);
        });
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

反应本机 firebase 检查用户是否已存在于实时数据库中 的相关文章

随机推荐