【问题标题】:Firebase query into nodesFirebase 查询到节点
【发布时间】:2017-10-17 11:16:21
【问题描述】:

一个问题,我该如何做这个查询?

架构

-|conversations
-|$uid
    -|fromID
        -|message
            name: string
            date: date
            body: string
            like: boolea

如果用户是消息的发送者,关注者都是conversations/$uid的集合,关注的用户在conversations/ANYTHING/$uid

我可以通过这种方式列出关注者this.followers = this.af.database.list(conversations/${this.currentAuth.uid});

但是,关注的用户呢?

添加此图以更好地解释

【问题讨论】:

  • fromID是被关注用户的ID?
  • 在来自ID的用户是关注者,但是同一用户将您的密钥插入到来自ID的另一个用户中,该用户是另一个用户的关注者

标签: angular firebase firebase-realtime-database angularfire2


【解决方案1】:

因此,使用 database.list,您会得到一组消息。

对于列表中的每条消息,您都添加了元数据。

您可以使用以下密钥获取消息的密钥(即跟随的用户 ID):"$key"

示例:

const message = this.af.database.list(conversations/${this.currentAuth.uid});

message[0].$key 会给你关注用户的 user_id。

编辑 1:

我真的不明白你要做什么。但是您在评论中的建议是:

If you use Lodash:

这将为您提供与 this.currentAuth.uid 作为 fromID 的所有对话:

_.filter(
    this.af.database.list(conversations),
    function (o) => { 
        return Object.keys(o).indexOf(this.currentAuth.uid) != -1 
    }

)

【讨论】:

  • 嗯,我不知道这是否有效。在架构中,如果我使用const message = this.af.database.list(conversations/${this.currentAuth.uid});,我将获得用户this.currentAuth.uid 的所有对话,如果您调用message[0].$key,您将获得-|fromID,这是您的关注者之一,不会被关注。对跟随的查询可能是这样的:我需要找到在节点对话中存在多少次currentAuth.uid 作为 fromID 参数
  • 如果你询问当前用户的对话,你会得到追随者,但如果你问...当前用户在对话中作为 fromID 存在多少次?您将获得关注的用户
【解决方案2】:

解决方案是添加两个节点。关注者并关注每个配置文件以保存所有密钥。

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多