【问题标题】:react native firebase check if user already exisits in real time database反应原生firebase检查用户是否已经存在于实时数据库中
【发布时间】:2019-10-13 12:38:01
【问题描述】:

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

signup.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')

                            }
                    })

【问题讨论】:

  • 显示用户数据的控制台日志
  • @JuniusL。控制台日志什么也不返回,它转到 else 语句。请帮忙
  • 控制台日志rootRef
  • @JuniusL。我将其更改为let rootRef = firebase.database().ref('users')。我如何在高性能的同时访问/循环所有 uid /account / username。我尝试了 .child(${uid}) 和 .child("account) 但它返回 null。
  • 那么rootRef cotain 是什么意思?

标签: javascript react-native firebase-realtime-database firebase-authentication


【解决方案1】:

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

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);
        });
    }
});

【讨论】:

  • 非常感谢它运行良好。感谢您昨天抽出时间来帮助我。
猜你喜欢
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多