javascript - 简单查询 Firebase 数据库

标签 javascript angularjs firebase firebase-realtime-database nosql

我在 Firebase 上创建了一个具有以下结构的数据库:

employees
    employee-id
        name: "Jhon Doe"
        bio: "Software Developer"
        birthday: "03/23/1986"
recognitions
    recognition-id
        date: "03/23/2017"
        employee: employee-id
        type: "Team Working"

如何从我的数据库中获取所有识别以及同一对象中的员工数据?

我正在使用 AngularJS 在网络上开发它。

最佳答案

您可以创建自己的对象,因为 Firebase 不具备数据库端联接的功能。

var bigObject = {};
// Find all recognitions
var ref = firebase.database().ref("recognitions");
ref.once("value").then(function(snapshot) {
    bigObject = snapshot.val();

    // Loop through all recognitions
    for(o in bigObject) {
        var ref2 = firebase.database().ref("employees").child(bigObject[o].employee);
        ref.once("value").then(function(snapshot2) {
            bigObject[o].employee = snapshat2.val();
        }
    }
});

你会得到这样的结果:

recognitions
    recognition-id
        date: "03/23/2017"
        employee
            name: "Jhon Doe"
            bio: "Software Developer"
            birthday: "03/23/1986"
        type: "Team Working"

如果您使用 JavaScript SDK(您没有标记 AngularFire),这将起作用

关于javascript - 简单查询 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983979/

相关文章:

javascript - 在图像翻转时调用随机音频文件

php - 使用 Ajax 调用同一个文件中的 PHP 对象方法

javascript - 如何知道模式框(警告、提示、确认...)是否已在 javascript 中被禁用?

javascript - 在 Angular 函数内调用函数

angularjs - 排队 GPT 命令中的异常类型错误 : Cannot read property 'addService' of null

带有按钮 : Disable button after click 的 AngularJS ng-repeat 列表

reactjs - 使用 firebase 部署 React 应用程序时如何隐藏源代码?

android - WebStorm 上的 Firebase 设置

android - 从 android 应用程序中完全删除 firebase 分析

javascript - 如何根据长度对带字符串的二维数组进行排序?