ccocker
ccocker

Reputation: 1236

AngularFire2 - Querying Data on $key

I am trying to write a query that will extract a single item from a collection based on it's key.

I have been searching and following alot of tutorials, but everything seems to just show how to get a list like below:

I would like to pass in the $key and query and pull a single record. Any suggestions or direction to a source that could help would be appreciated.

import { Injectable } from '@angular/core';

import { Customer } from "./customer";

import { AngularFire, FirebaseListObservable} from 'angularfire2';

@Injectable()
export class CustomerService {

    customer: Customer;
    customers: FirebaseListObservable<Customer[]>;
    categories: FirebaseListObservable<Category[]>;

    constructor(private af: AngularFire) { }

    getCustomer(customerIndex: number) {

        this.customers = this.af.database.list('customer');

        return this.customers;
    }
}

Upvotes: 2

Views: 3121

Answers (1)

Rob Gorman
Rob Gorman

Reputation: 3694

If you know the key you can do this:

this.af.database.object('/customers/' + key)
  .subscribe(customer =>{
     // Assuming that name is a value of customer you can say
     var name = customer.name; 
     ...
  }

Of course this is assuming that "customers" is the list of customers that you have previously pushed.

Upvotes: 4

Related Questions