我有一个JSON结构
{
"engagedUsers": {
"qwe": {
"agent_availability": true,
"conv_id": "parthengineer_qwe",
"emailId": "qwe@gmail.com",
"login_time": 1512644440,
"name": "qwe",
"uid": "adasfklsfjdskldgsgjgjkl"
},
"tt": {
"agent_availability": true,
"conv_id": "genesis_tt",
"emailId": "tt@gmail.com",
"login_time": 1512644440,
"name": "tt",
"uid": "adasfklsfjdskldgsgjgjkl"
}
}
}
我想验证conv_id孩子是否等于 parthengineer_qwe 。我尝试了以下代码,但无法通过它:
this.engagedRef = firebase.database().ref('engagedUsers');
this.engagedRef.orderByValue().once('value', function (snapshot) {
snapshot.forEach(function (data) {
// console.log("snapShot:" + data.child('conv_id').val());
if ((data.child('conv_id').val() == 'parthengineerqwe') && (existEngageduser !== 1)) {
existEngageduser = 1
// console.log("under haschild:" + existEngageduser);
}
if (existEngageduser == 1) {
isEngaged = true
// sessionStorage.isEngaged = isEngaged;
}
// console.log("isEngaged:" + isEngaged);
})
return isEngaged;
});
请建议一个更好的方法来实现这一目标,因为我知道我的方法不正确,我希望isEngaged变量为真
答案 0 :(得分:0)
我还不确定你想要什么,但为什么没有外部功能呢?
snapshot.forEach(function (data) {
if (isEngaged(data.val())) {
console.log(data.key, ' is engaged');
}
}
function isEngaged(userData) {
return (userData.conv_id === 'parthengineer_qwe') || false;
}