简体   繁体   English

React-Native:Firebase查询父值

[英]React-Native: Firebase Query for parent values

I am completely new to react native and javascript. 我是本机和JavaScript的新手。 I am trying to search the database for an entered number, wich is an child of the users node. 我正在尝试在数据库中搜索输入的数字,这是用户节点的子代。 If the query have found the entered number I want to know if the number is verified. 如果查询找到了输入的号码,我想知道该号码是否已验证。 I see all values when I debug that belongs to the user, but I cannot access it. 调试属于用户的所有值,但无法访问。 How do I get the userID value of from my point? 如何从我的角度获取userID值?

 getUserByPhoneNumber = phoneNumber => {
var ref = firebase.database().ref("/Users");
var query = ref.orderByChild("phone").equalTo("phoneNumber");
query.once("value", function(snapshot) {
  // User found
  if (snapshot.val()) {
    // undefined
    console.log(snapshot.val().phoneVerified);

    // output = "Users"
    console.log(snapshot.key)
  }
});

}; };

Firebase Datastructure is like this: Firebase数据结构是这样的:

/Users
    /uid
        /phone
        /phoneVerified

I'm assuming your FB structure is 我假设你的FB结构是

/Users
  /phone
    /phoneNumber
    /phoneVerfied

Function: 功能:

getUserByPhoneNumber = phoneNumber => {
  var ref = firebase.database().ref("/Users");
  var query = ref.orderByChild("phone/phoneNumber").equalTo(phoneNumber);
  query.once("value", function(snapshot) {
    const record = metadata.val();

    if (record) {
      const uids = Object.keys(record);

      // Modify this if you can have multiple records per phone number
      if (uids && uids.length > 0 && record[uids[0]].phone.phoneVerified) {
        // Do something with uid
      }
    }
  });
};

I solved this by itarating through the snapshot.val() 我通过在snapshot.val()中设置来解决了这个问题

  getUserByPhoneNumber = phoneNumber => {
var ref = firebase.database().ref("/Users");
var query = ref.orderByChild("phone").equalTo(phoneNumber);
query.once("value", function(snapshot) {
  // User found
  if (snapshot.val()) {
    // Whole user Node is returned - so foreach
    snapshot.forEach(function(childSnapshot) {
      var userID = childSnapshot.key;
      var userData = childSnapshot.val();
      if (userData.phoneVerified) {
        // User is known and Verified -> enter password
      } else {
        // User is known but not Verified -> send Verification
      }
    });
  } else {
    // User NOT found -> register
  }
});

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM