0

I am implementing dynamic routes in Realtime Database of Firebase based on the ID or $key of each object. What I want is to get the ID and I can not find the way, I get the value undefinied. Any ideas?

enter image description here

enter image description here

portafolio.component.html

<div class="container my-5">
<h1>Portafolio</h1>
<div class="row">
    <div class="col-md-4" *ngFor="let proyecto of proyectos | async">
        <div class="card my-3">
            <div class="card-body">
                <h4 class="card-title">{{ proyecto.titulo }}</h4>
                <a class="btn btn-primary" [routerLink]="['/portafolio', proyecto.$key]">Ver detalles</a>
            </div>
        </div>
    </div>
</div>

portafolio.component.ts

import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from 'angularfire2/database';


@Component({
  selector: 'app-portafolio',
  templateUrl: './portafolio.component.html',
  styleUrls: ['./portafolio.component.scss']
})
export class PortafolioComponent implements OnInit {

  proyectos: any;

  constructor(private db: AngularFireDatabase) { }

  ngOnInit() {

      this.proyectos = this.db.list('proyectos').valueChanges();

  }

}

proyecto.ts

export interface Proyecto {
$key?: string;
titulo?: string;
destacado?: string;
descripcion?: string;}
2
  • 3
    You need to use snapshotChanges to get the key. Check this answer Commented Dec 27, 2017 at 14:39
  • Excellent worked for me with snapshotChanges () Commented Dec 28, 2017 at 0:39

2 Answers 2

1

Thanks to @Hareesh for sharing the related topic: https://stackoverflow.com/a/47291970/8312532

This worked for me:

portafolio.component.ts

import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, AngularFireList} from 'angularfire2/database';
import { Observable } from "rxjs/Observable";



@Component({
  selector: 'app-portafolio',
  templateUrl: './portafolio.component.html',
  styleUrls: ['./portafolio.component.scss']
})
export class PortafolioComponent implements OnInit {

  todosProyectos: AngularFireList<any>;
  proyectos: Observable<any[]>;

  constructor(private db: AngularFireDatabase) { }

  ngOnInit() {

    this. todosProyectos = this.db.list('proyectos');

    this.proyectos = this.todosProyectos.snapshotChanges().map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
    });


  }

}

portafolio.component.html

<div class="container mt-5">
<h1>Portafolio</h1>
<div class="row">
    <div class="col-md-4" *ngFor="let proyecto of proyectos | async">
        <div class="card my-3">
            <div class="card-body">
                <h4 class="card-title">{{ proyecto.titulo }}</h4>
                <a class="btn btn-primary" [routerLink]="['/portafolio', proyecto.key]">Ver detalles</a>
            </div>
        </div>
    </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Use the square bracket to access the object property, proyecto['$key']

<a class="btn btn-primary" [routerLink]="['/portafolio', proyecto['$key']]">Ver detalles</a>

10 Comments

are you sure? I'm still coming out undefined
post the proyecto object
I have included the project project.ts
print {{ proyecto }} in the html and post the result
the result is [object Object]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.