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

时间:2017-01-18 11:29:58

标签: javascript json firebase-realtime-database

我有以下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}

2 个答案:

答案 0 :(得分:1)

获取childSnapshot调用的密钥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());
    })
})

答案 1 :(得分:0)

REST API中现在有一个浅层命令,它只获取路径的键。这还没有添加到SDK中。

在Firebase中,如果不检索下面的数据,则无法获取节点名称列表。反正还没有。性能问题可以通过规范化来解决。

基本上,您的目标是将数据拆分为可消耗的块。存储您的视频密钥列表,可以在一个路径中使用标题等几个元字段,并将批量内容存储在其他位置。对于 '/ video_meta / id / link,title,... / video_lines / ID /...'

要了解有关非规范化的详情,请查看此文章:https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.htm

要获取任何快照的“名称”(在这种情况下,push()创建的ID),请像这样调用name()

var name = snapshot.name();

如果您想获取push()自动生成的名称,您只需在返回的引用上调用name(),就像这样:

var newRef = myDataRef.push(...);
var newID = newRef.name();