如何检查Firebase数据库中是否存在项目? - react-native-firebase

时间:2017-12-04 08:35:49

标签: firebase react-native firebase-realtime-database react-native-android react-native-ios

目前,我正在为我的项目使用https://github.com/invertase/react-native-firebase。我有一个用户自定义数据库,我想通过电子邮件检查用户是否存在。

以下是数据库的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个通用的firebase方法,但您可能需要重新配置该方法以适合您的数据结构。如果您想了解更多信息,请参阅official docs

firebase.database()
    .ref(`/users`)
    .orderByChild("email")
    .equalTo(email)
    .once("value")
    .then(snapshot => {
        if (snapshot.val()) {
            // data exist, do something else
        }
    })

您还可以使用hasChild方法查询注册状态。请参考您的根路径并使用.once进行查询,并检查返回的结果。

export function checkUserExist(email) {
  return(dispatch) => {
    firebase.database().ref(`/ExistingUser/`)
      .once('value', snapshot => {
        if(snapshot.hasChild(email)) {
          dispatch({
            type: FIREBASE_USER_EXISTED            
          });
        } else {
          dispatch({
            type: FIREBASE_USER_NOT_EXISTED,
          });
        }
      });
  }
}

另一种首选方法是使用Firebase提供的fetchProvidersForEmail方法。它需要一封电子邮件并返回一个承诺,该承诺通过链接到该电子邮件的提供商列表进行解析(如果已经注册)refer here

是否有充分的理由在您的数据库中存储用户凭据?在我的日常练习中,出于安全考虑,我会使用Firebase提供的createUserWithEmailAndPassword refer here。只需确保正确定义规则以防止未经授权的访问。