1

This is my first project in Angular4 + Firebase, I would like your support about some questions. I'm using AngularFire2 to connect with Firebase server.

This is my Firebase database:

product
  |-KmlWSH-U3XTwudPsA4X 
    |name: "product 1" 
    |type: "52"
    |manufacture: "-KtJkcbXP5dOcOeoP20G"  
  |-KmlXXX-XXXXXX 
    |name: "product 2" 
    |type: "53"
    |manufacture: "-XXXXXXXXXXXXXXXXXX" 

manufacture
  |-KtJkcbXP5dOcOeoP20G 
    |name: "manufacture 1" 
    |country: "Japan"

I'm trying to get a list of products joined with the properly manufacture object. I expect to receive something like this:

  {$key: -KmlWSH-U3XTwudPsA4X 
  name: "product 1" 
  type: "52"
  manufacture: "-KtJkcbXP5dOcOeoP20G"  
  manufactureObject: {name: "manufacture 1", country: "Japan"} }

Currently I'm using the follow code to retrieve this:

return this.db.list("product")
.map(products => products.map(product => {
        let p = Product.fromJson(product);
        p.manufactureObject = this.db.object("manufacture"+ "/" +p.manufacture);
        return p;
      }))

"manufactureObject" returns as Observable in this case.

On the template I have no problem with that, I'm using

{{ (row.manufactureObject | async)?.name}}

Works like a charm.

But I'm don't know how to use it on FormGroup.

this.form = new FormGroup({
      '$key': new FormControl(t.$key),
      'name': new FormControl(t.name, Validators.required),
      'manufactureObject': new FormControl(t.manufactureObject, Validators.required),
    });

Has anyone know how to do it? Is there any other way to do it?

In SQL this is so simple, it is just a "join" with another table ^^

Thanks for your support

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
rickdroio
  • 505
  • 4
  • 6
  • 1
    The Firebase Database has no support for server-side joins. Instead you do the join on the client. See https://stackoverflow.com/questions/38421551/joining-flattened-data for an example and http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 for why this isn't as slot as you may think. – Frank van Puffelen Sep 14 '17 at 18:37
  • Dear Frank, actually I found these posts, I've tried to follow the https://stackoverflow.com/questions/38421551/joining-flattened-data, it is very similar I'm doing. But I got the follow issue: "Cannot read property 'map' of undefined". How come a property can have this method available? it would be perfect! PS: I already declared "import 'rxjs/add/operator/map'" on my app.module. Thank you – rickdroio Sep 14 '17 at 20:01
  • When I had that problem and error message, it was literally a missing import. https://stackoverflow.com/questions/39984247/how-do-i-add-client-side-attributes-to-items-in-a-firebaselistobservable Not sure what it could be in your case. – Frank van Puffelen Sep 15 '17 at 01:30

0 Answers0