最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase: joining tables - Stack Overflow

programmeradmin1浏览0评论

Assume that the data look like this

post: {
  authorId: '123AA',
  title: 'first Post',
  body: 'yay'
}

author: {
  uid: '123AA',
  displayName: 'Richard'
  email: '[email protected]'
}

I want to render a post with the author's name:

<div className="post">
  <div className="title">{post.title}</div>
  <div className="body">{post.body}</div>
  <div className="author-name">{post.author.displayName}</div> //problem
</div>

A fetched post item only contains an author's uid, but I need the author's displayName. How do I populate the author field when retrieving the posts? This is what I do to fetch the posts right now:

const postsRef = firebase.database().ref('posts/')
postsRef.on('value', (snapshot) => {
  this.setState({posts: snapshot.val()})
})

Assume that the data look like this

post: {
  authorId: '123AA',
  title: 'first Post',
  body: 'yay'
}

author: {
  uid: '123AA',
  displayName: 'Richard'
  email: '[email protected]'
}

I want to render a post with the author's name:

<div className="post">
  <div className="title">{post.title}</div>
  <div className="body">{post.body}</div>
  <div className="author-name">{post.author.displayName}</div> //problem
</div>

A fetched post item only contains an author's uid, but I need the author's displayName. How do I populate the author field when retrieving the posts? This is what I do to fetch the posts right now:

const postsRef = firebase.database().ref('posts/')
postsRef.on('value', (snapshot) => {
  this.setState({posts: snapshot.val()})
})
Share Improve this question edited Aug 5, 2016 at 14:24 Frank van Puffelen 600k85 gold badges890 silver badges860 bronze badges asked Aug 5, 2016 at 12:29 Maximus SMaximus S 11.1k19 gold badges80 silver badges163 bronze badges 6
  • 1 Its better to remove reactjs from the tags, the question has nothing to do with React – webdeb Commented Aug 5, 2016 at 12:34
  • 1 As far as I know you can't make "joins" with firebase, you need to make a second request to get something from another collection – Rafael Teles Commented Aug 5, 2016 at 12:57
  • 1 so is that what people do? For every post that gets added, you make another request to get the user object from the server? – Maximus S Commented Aug 5, 2016 at 12:58
  • 1 Check here a "join" example of firebase: firebase./docs/web/guide/structuring-data.html#section-join – Rafael Teles Commented Aug 5, 2016 at 13:05
  • 1 @MaximusS a join indeed requires an extra call. These extra calls are not as expensive as most developers think, because Firebase pipelines the requests over its existing connection. – Frank van Puffelen Commented Aug 5, 2016 at 14:25
 |  Show 1 more ment

1 Answer 1

Reset to default 8

Not sure in real firebase, but I use join quite often in angular2. Here my sample code.

import { Component, OnInit } from '@angular/core';
import { AngularFire } from 'angularfire2';
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/map';

@Component({
  selector: 'app',
  templateUrl: `
    <ul>
        <li *ngFor="let p of posts | async">
            {{ p.title }} - {{ (p.authorName | async)?.displayName }}
        </li>        
    </ul>
`
})
export class InitComponent implements OnInit {
    posts: Observable<any[]>;

  constructor(private af: AngularFire) {}

  ngOnInit() {
      this.posts = this.af.database.list('/posts')
      .map(posts => {
          posts.map(p => {
              p.authorName = this.af.database.object('/authors/'+p.authorId);
          });
          return posts;
      });
  }

The database structure as follows:

/posts/postId/{authorId, title, body}
/authors/authorId/{displayName, email}

Hope this helps

发布评论

评论列表(0)

  1. 暂无评论