简体   繁体   English

AngularFire2-在$ key上查询数据

[英]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. 我想传入$ key并查询并提取一条记录。 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;
    }
}

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. 当然,这是假设“客户”是您先前推送的客户列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM