2

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;
    }
}

1 Answer 1

4

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.

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

2 Comments

Thank you - I new it would be simple, really appreciate your help
By any chance do you know how to do it based on a subkey? I have "sites/any/es/know" where known is the key I want to query for, and any cannot be known beforehand, is there any way besides just retrieving the first array and filtering it?

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.