简体   繁体   English

如何在angularfire2中查询

[英]How to Query in angularfire2

while using angularfire in my project i didnt know what to write exactly to get the name bellow MY BD在我的项目中使用 angularfire 时,我不知道该写什么才能得到我的 BD名称

i really need help, i want the query that gives me back the key of the item where name="om" for example thanks !我真的需要帮助,我想要查询,让我返回名称=“om”的项目的键,例如谢谢!

What you're trying to do here is, Query Lists Just have a look at the Docs and you should be on your way.您在这里尝试做的是, 查询列表只需查看文档,您就可以开始了。

Here's a simple example to get you started:这是一个让您入门的简单示例:

import { Component } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  users;

  constructor(private db: AngularFireDatabase) {}

  ngOnInit() {
    this.db.list('/Users', ref => ref.orderByChild('name').equalTo('Om'))
      .snapshotChanges()
      .subscribe((snapshot: any) => this.users = snapshot.map(snap => ({ ...snap })));
  }

}

And in the Template:在模板中:

<pre>{{ users | json }}</pre>

This will return an array.这将返回一个数组。 So you'll have to make sure that the field that you're querying is unique if you want only one item.因此,如果您只需要一个项目,则必须确保您查询的字段是唯一的。

Also, this sort of query will download the whole data on the client.此外,这种查询将下载客户端上的全部数据。 So you'd get a warning saying:所以你会收到一条警告说:

@firebase/database: FIREBASE WARNING: Using an unspecified index. @firebase/database:FIREBASE 警告:使用未指定的索引。 Your data will be downloaded and filtered on the client.您的数据将在客户端下载和过滤。 Consider adding ".indexOn": "name" at /Users to your security rules for better performance.考虑将 /Users 处的 ".indexOn": "name" 添加到您的安全规则中以获得更好的性能。

To fix that, update your security rules like this:要解决这个问题,请更新您的安全规则,如下所示:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "Users": {
      ".indexOn": [
        "name"
      ]
    }
  }
}

Here's a Working Sample StackBlitz for your ref.这是供您参考的工作示例 StackBlitz

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

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