以下代码检查 ID U1EL5623 是否存在。
然而,我需要一个额外的条件。
如果 fName 也等于John,它应该只返回 true
有没有办法根据另一个项目的值检查 a 值是否存在?
代码
ref.child("users").orderByChild("ID").equalTo("U1EL5623").once("value",snapshot => {
const userData = snapshot.val();
if (userData){
console.log("exists!");
}
});
数据
{
"users": {
"-KKUmYgLYREWCnWeHCvO": {
"fName": "Peter",
"ID": "U1EL9SSUQ",
"username": "peter01"
},
"-KKUmYgLYREWCnWeHCvO": {
"fName": "John",
"ID": "U1EP7532",
"username": "john.doe"
},
"-UIgmYgLWDRWCnWeHCpF": {
"fName": "Alex",
"ID": "U1EL5623",
"username": "Alex.JP"
}
}
}
获得值后,您可以遍历所有结果及其对象,然后您还可以像这样检查 fName 是否等于 John:
ref.child("users").orderByChild("ID").equalTo("U1EL5623").once("value",snapshot => {
const userData = snapshot.val();
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
if (userData && childData.fName === 'John'){
console.log("exists!");
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句