【问题标题】:AngularFire2 - Querying Data on $keyAngularFire2 - 在 $key 上查询数据
【发布时间】:2016-09-14 17:15:49
【问题描述】:

我正在尝试编写一个查询,该查询将根据其键从集合中提取单个项目。

我一直在搜索和关注很多教程,但一切似乎都只是展示了如何获得如下列表:

我想传入 $key 并查询并提取一条记录。任何可以提供帮助的来源的建议或指导将不胜感激。

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

【问题讨论】:

    标签: angular angularfire2


    【解决方案1】:

    如果你知道密钥,你可以这样做:

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

    当然这是假设“客户”是您之前推送的客户列表。

    【讨论】:

    • 谢谢你-我很简单,非常感谢你的帮助
    • 你有没有机会知道如何基于子键来做到这一点?我有“sites/any/es/know”,其中 known 是我要查询的键,并且无法事先知道 any,除了检索第一个数组并对其进行过滤之外,还有其他方法吗?
    猜你喜欢
    • 2017-01-31
    • 2018-06-14
    • 1970-01-01
    • 2018-02-19
    • 2023-03-17
    • 2018-06-22
    • 2019-01-03
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多