Firebase and Angular2

Asked

Viewed 293 times

0

all right?

I don’t know what’s left to get the answer from Firebase using Angular2. I followed a tutorial that worked, but when trying in my project, I get 'null'. I don’t know if I use Firebaseobjectobservable or Firebaselistobservable. Well, let’s go to the code.

import { Component } from '@angular/core';
import { AngularFire, FirebaseObjectObservable  } from 'angularfire2';

@Component({
  selector: 'app-marcas',
  templateUrl: './marcas.component.html'
})

export class MarcasComponent {
    meufirebase: FirebaseObjectObservable<any[]>;
    constructor(af: AngularFire) {
    this.meufirebase = af.database.object('/marcas/marca_1/nome');
  }
}

And my html:

<label>Escolha a marca</label>
<select materialize="material_select" [materializeSelectOptions]="marcas" class="browser-default">
  <option  *ngFor="let marca of marcas">{{ marcas.nome }}</option>
</select>

And a picture of how I set up my firebase

inserir a descrição da imagem aqui

I’m waiting for a little help from some guru! A hug!

I edited a part of the component code for:

  constructor(af: AngularFire) {

    const queryObservable = af.database.list('/marcas', {
      query: {
        orderByChild: 'nome'
      }
    });
    // subscribe to changes
    queryObservable.subscribe(queriedItems => {
      console.log(queriedItems);  
    });
  }
}

And now on the console I have:

inserir a descrição da imagem aqui

I mean, I’m getting to Firebase, but something is missing, because I don’t understand these Object that should be marca_1, marca_2, marca_3 and marca_4 according to the structure of my bank...

  • Make sure you put your database when you created it with the country Brazil ... in one I selected Brazil, did not work. I had to create another one and put America.

  • Really? I’ll try, but I think the solution is unlikely.

  • As I thought, nothing has changed. But thank you for your reply!

1 answer

1


My collection at firebase

inserir a descrição da imagem aqui

My component

import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  marcas: FirebaseListObservable<any []>;

  constructor(private af: AngularFire) {
    this.marcas = af.database.list('/Marcas');
    console.log(this.marcas);
  }
}

My View

<h1>
  {{title}}
</h1>
<hr>
<ul>
  <li *ngFor="let marca of marcas | async">
    {{marca.$key}} {{ marca.name}}
  </li>
</ul>

inserir a descrição da imagem aqui

  • YES!!!! YOU ARE THE MAN! Thanks a lot, buddy! I discovered that in *ngFor= "Let mark of marks", I was calling {{ marks.name }} instead of {{mark.name }} .

  • Beauty... it was easier than I imagined. I already have two select reading from firebase. Now, would you tell me how to make the second select, which is of models, correspond only to a single brand? For example: Now I’m bringing All brands in the first select. But when selecting VW, I would like select models to only bring VW models, you know?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.