对角度为2

时间:2016-08-13 20:34:00

标签: angular ionic-framework firebase firebase-realtime-database ionic2

我是firebase的新手并尝试从中检索数据。我有 denormalize 我的数据。每条评论都有一个userId,每个用户都拥有所有评论ID。

我能够检索单个评论或用户,但我不知道如何列出用户的所有评论。

这是我检索用户1(插孔)的代码:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import {AngularFire, FirebaseListObservable, FirebaseObjectObservable} from 'angularfire2';


@Component({
    templateUrl: 'build/pages/user-page/user-page.html',
})
export class FousiorPage {
    user: any;
    comments: any;


    constructor(
        private navCtrl: NavController,
        private af: AngularFire

    ) {

        af.database.object('/users/1').subscribe(result => {

            this.user = result;
            console.log("this.user: ", this.user);

            //this.comments = ???
            //How to get all the comments from firebase for user/1 Jack?
            console.log("this.comments: ", this.comments);  

        });
    }
}

这是我导入firebase的json树。

{
    "comments": [
        {
            "content": "How are you?",
            "author": {
                "0": true
            }
        },
        {
            "content": "I am good. Last night dinner was fantastic",
            "author": {
                "1": true
            }
        },
        {
            "content": "What did you have?",
            "author": {
                "2": true
            }
        },
        {
            "content": "It was a French dish made by Joanna's mum.",
            "author": {
                "1": true
            }
        }
    ],
    "users": [
        {
            "name": "John",
            "comments": {
                "0": true
            }
        },
        {
            "name": "Jack",
            "comments": {
                "1": true,
                "3": true
            }
        },
        {
            "name": "Kat",
            "comments": {
                "2": true
            }
        }
    ]
}

这是我在Firebase中获得的结果:

Firebase data

1 个答案:

答案 0 :(得分:0)

您可以在列表中进行过滤。

users.filter(function(user){
return user.id == user.comment.id
})