我对原生和javascript做出反应是全新的。我正在尝试在数据库中搜索输入的数字,它是用户节点的子节点。如果查询已找到输入的号码,我想知道该号码是否已经过验证。我在调试时看到属于用户的所有值,但我无法访问它。如何从我的角度获取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数据结构是这样的:
/Users
/uid
/phone
/phoneVerified
答案 0 :(得分:1)
我假设你的FB结构是
/Users
/phone
/phoneNumber
/phoneVerfied
功能:
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
}
}
});
};
答案 1 :(得分:1)
我通过flash.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
}
});
};