AngularFire2:按属性值检索双重嵌套对象 [英] AngularFire2: Retrieve Double Nested Object by Property Value

查看:28
本文介绍了AngularFire2:按属性值检索双重嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但我很难做到这一点.考虑以下模型:

I'm not sure why, but I'm having a hard time with this. Consider the following model:

threads
    threadId
        posts
            postId
                author: authorId

这种结构对我来说很有意义,因为我想在 ngFor 中的线程视图中呈现帖子.现在我想在他们的个人资料视图中显示由特定用户撰写的帖子列表.但是我很难弄清楚如何将它们放入可观察列表中.

This structure made sense for me, as I wanted to render the posts in the thread view within ngFor. Now I want to display lists of posts written by specific users in their profile view. But I'm having a hard time figuring out how to get them into an observable list.

因为我想要的对象的路径中有多个键,所以我必须做很多映射才能到达那里.但 Firebase ref 路径引用的列表中的列表似乎不可迭代.因此,例如:

Since there are multiple keys in the path to the object I want, I have to do a lot of mapping to get there. But it seems like lists within the list referred to by the Firebase ref path aren't iterable. So, for example:

getPosts(uid: string): Observable<any> {
    let results = this.af.database.list(`/threadsMeta`)
    .do(threads => {
        threads.forEach(thread => {
            let posts = thread.posts
            posts.forEach(post => {
                // Not even sure what I'd do here,
                // but posts isn't iterable, so it's moot
            })
        })
    })
    return results
}

这是有道理的,因为它是一个对象,而不是一个数组.这个怎么样?

This makes sense since it's an object, not an array. How about this?

getPosts(uid: string): Observable<any> {
    let results = this.af.database.list(`/threadsMeta`)
    .do(threads => {
        threads.forEach(thread => {
            return this.af.database.list(`threadsMeta/${thread.$key}/posts`)
            .map(posts => posts.filter(post => post.author === uid))
        })
    })
    results.subscribe(data => console.log(data))
    return results
}

没有.这不会过滤任何内容.

Nope. This doesn't filter anything.

我需要一个 author 属性等于 uid 参数的帖子列表.

I need a list of posts whose author properties are equal to the uid param.

现在我正在做这个,它有效,但感觉非常难看.

Right now I'm doing this, and it works, but it feels awfully ugly.

getPosts(uid: string): Observable<any> {
    let posts: any[] = []
    return this.af.database.list(`/threadsMeta`)
    .map(threads => {
        threads.forEach(thread => {
            for (var post in thread.posts) {
                if (thread.posts[post].author === uid) {
                    posts.push(thread)
                }
            }
        })
    return Observable.of(posts)
    })
}

推荐答案

试试这个:

getPosts(uid: string): Observable<any> {
    return this.af.database.list(`/threadsMeta`)
    .map(threads => {
        let posts = [];
        threads.forEach(thread => {
            posts = thread
                      .posts
                      .filter(post => {
                         posts => posts.filter(post => post.author === uid)
                      })
        });
        return posts;
    })
}

你这样称呼它:

getPosts("123").sucbscribe(posts=>{
   ///posts
});

这篇关于AngularFire2:按属性值检索双重嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆