gpt4 book ai didi

javascript - 根据 emailId 匹配从 Firebase 数据库获取节点名称

转载 作者:行者123 更新时间:2023-12-01 04:07:30 24 4
gpt4 key购买 nike

我有下面的 Json 架构,我可以使用什么 firebase 查询来获取节点名称(0153,0154),我可以使用一次方法获取名称(joel,vikram,sachin)

 "EmployeeInfo":
{
"0153":
{
"Name":"Joel",
"Dept":"Engineering",
"Email":"joel@dept.com"
},
"0163":
{
"Name":"Vikram",
"Dept":"Engineering",
"Email":"vikram@dept.com"
},
"0173":
{
"Name":"Sachin",
"Dept":"Engineering",
"Email":"Sachin@dept.com"
}

这是我的 JavaScript 函数:

function Call() {

var dbref = new Firebase("https://logintrialapp.firebaseio.com/Employee/EmployeeInfo");
var login = localStorage.getItem("Email");
dbref
.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childData = childSnapshot.val();
name = childData.Name;
var n = login.localeCompare(name);
console.log(name);

if (n != 0) {
var eid = snapshot.val();
console.log("Hello" + eid);
}
console.log(snapshot.val());
})
})

使用此代码,我可以获取名称,但我想检索节点名称,可以使用什么查询来根据 emailid 匹配条件获取节点名称

输出:

Joel
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object {0153: Object, 0163: Object, 0173: Object}0153: ObjectDept: "Engineering"Email: "joel@dept.com"Name: "Joel"__proto__: Object0163: Object0173:
Object__proto__: Object
Checkworks.html:44 Vikram
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object0153: ObjectDept: "Engineering"
Email: "joel@dept.com"Name: "Joel"__proto__: Object0163
: Object0173: ObjectDept: "Engineering"Email: "Sachin@dept.com"Name: "Sachin"__proto__: Object__proto__: Object
Checkworks.html:44 Sachin
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object {0153: Object, 0163: Object, 0173: Object}

最佳答案

要获取 childSnapshot 的 key ,请调用 childSnapshot.key:

.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childKey = childSnapshot.key; // Get the key of the child
childData = childSnapshot.val();
name = childData.Name;
var n = login.localeCompare(name);
console.log(name);

if (n != 0) {
var eid = snapshot.val();
console.log("Hello" + eid);
}
console.log(snapshot.val());
})
})

关于javascript - 根据 emailId 匹配从 Firebase 数据库获取节点名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com