【问题标题】:Firebase - Get all items where a field called uid equals uid?Firebase - 获取名为 uid 的字段等于 uid 的所有项目?
【发布时间】:2015-12-15 22:15:57
【问题描述】:

我有一个 Firebase 表,它是一个任务列表。

任务有一个名为 uid 的字段。

我想获取我传入的 uid == uid 的所有任务。

【问题讨论】:

  • 听起来不错的要求。你已经尝试过什么了吗?因为这是相当基本的Firebase querying
  • 是的,我尝试了一些方法并阅读了文档。我基本上是在尝试做与 sql where 语句等效的操作
  • 例如获取所有 uid = uid 的任务

标签: javascript firebase firebase-security firebase-authentication


【解决方案1】:

Firebase 允许您对一组数据进行排序然后过滤。

JSBin demo

var ref = new Firebase('https://<my-firebase-app>.firebaseio.com/items');
var query = ref.orderByChild('uid').equalTo('1'); 
query.on('value', (snap) => console.log(snap.val()));
  1. 按方法指定要使用的顺序。有很多选择,但在这种情况下,您需要按子键排序,因此请使用orderByChild(key)
  2. 指定排序依据,在本例中为uid
  3. 使用过滤功能限制字段集,在本例中为equalTo()。将字符串 '1' 替换为您需要的 uid

在你的情况下,它会是这样的:

var query = this.refJob.orderByChild('uid').equalTo(this.uid);
query.on('value', (snap) => console.log(snap.val()));

Read the docs on querying for more information.

【讨论】:

  • 我用了上面的例子,我的打字稿编译器说找不到密钥?
  • 嗨,大卫,还有一件事,我如何通过密钥获取?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 2023-04-02
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多