简体   繁体   English

angular angularfire2简单连接

[英]angular angularfire2 simple join

i'm trying to create a post-comment relationship​​ where the a user can write a post and others users can comment on the post. 我正在尝试创建评论后关系,用户可以在其中发表帖子,其他用户可以对帖子发表评论。

I can show the posts but when in trying to do the join for displaying the comments that belongs to the post i cant.. below is my db schema 我可以显示帖子,但是在尝试进行联接以显示属于该帖子的评论时,我无法..以下是我的数据库架构

火力数据库

i was thinking that first i need to get the key from the posts node and then move to comments and somehow get the comments of each post.. and use it in *ngfor inside the ngfor of the post? 我在想,首先我需要从posts节点获取密钥,然后移至评论,并以某种方式获取每个post的评论..并在帖子的ngfor内的* ngfor中使用它?

i was trying something like 我正在尝试类似

findAllComments(){

   this.db.list('posts', { preserveSnapshot: true})

    .subscribe(snapshots=>{
        snapshots.forEach(snapshot => {
          return this.db.list(`comments/${snapshot.key}`)
        });

    });
}

but this returns void of course: 但这当然无效:

When I console.log: 当我console.log时:

findAllComments(){

   this.db.list('/posts', { preserveSnapshot: true})

    .subscribe(snapshots=>{
        snapshots.forEach(snapshot => {
            const kapa = this.db.list(`comments/${snapshot.key}`).do(console.log)
            kapa.subscribe();
        });
    });
 }

I get in console this 我进入控制台

console.log

I'm not sure if my thinking on this is right. 我不确定我对此是否正确。

I'm confused because I am new in angular and firebase. 我很困惑,因为我是angular和firebase的新手。

You aren't returning a subset of posts (you're querying on all posts) so there's no need to have a join of any sort here. 您不会返回帖子的子集(正在查询所有帖子),因此这里不需要进行任何形式的联接。 You can just query for all comments: 您可以查询所有评论:

findAllComments(){
  // {preserveSnapshot: true} is deprecated
  return this.db.list('/comments').snapshotChanges(); 
}

Assuming you actually want to retrieve a subset of comments (not what your example depicts), you could do something like this: 假设您实际上想要检索注释的子集(而不是示例所描述的内容),则可以执行以下操作:

this.replies = db.list('AngularFire/joins/messages').snapshotChanges().map(snapshots => {
  console.log('snapshots', snapshots);
  return snapshots.map(ss => {
    return db.list(`AngularFire/joins/replies/${ss.key}`).valueChanges();
  });
});

There is a complete working example of the latter here . 还有就是后者的一个完整的工作示例在这里

I guess in the first part, you are not subscribing to the comments list. 我想在第一部分中,您不会订阅评论列表。 As there is no subscription to the comments, the request to the get the list of comments from firebase will not be fired and hence you don't see any comments. 由于没有订阅评论,因此不会触发从firebase获取评论列表的请求,因此您看不到任何评论。

In the second part, as you are subscribing to the comments list, you are seeing them. 在第二部分中,当您订阅评论列表时,您会看到它们。

In cases like these, where you want to fetch something based on a previous request, you could use switch/concat/merge Maps. 在这种情况下,如果您想根据先前的请求获取内容,则可以使用switch / concat / merge Maps。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM