【问题标题】:How to query an array of objects contains a field value firebase angular?如何查询包含字段值firebase angular的对象数组?
【发布时间】:2020-04-25 16:20:06
【问题描述】:

我有以下文件:

"players" : [ 
  {
    "id" : userid,
    "name" : "dissident"
  }, 
  {
    "teamId" : teamId,
    "name" : "ink"
  }
]

这是我尝试过的,但它不起作用

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId}))

【问题讨论】:

  • 您能否详细说明您的预期输出是什么?
  • 错误是什么?
  • @Mridul 我只在玩家数组包含给定团队 id 时才尝试获取此文档
  • 您是否将 JSON 称为文档?如果 teamid 与给定的团队 id 匹配,您想返回整个对象吗?
  • 您可能对这个答案感兴趣:stackoverflow.com/questions/54081799/… 它解释了使用数组包含来查询存储在数组中的对象的特定属性是不可能的

标签: angular firebase google-cloud-firestore


【解决方案1】:

要解决这个问题,请更改以下代码行:

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId}))

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId, name : 'ink'}))

如您所见,这实际上只有在您查询整个对象时才有可能。但请注意,正如 Doug Stevenson 在他的回答中提到的,如果您为 teamIdname 传递了 undefined 的无效值,您将始终收到您所说的错误。

【讨论】:

  • 嗨桑莎!您是否尝试过使用整个对象查询集合,它有效吗?如果您认为我的回答对您有所帮助,请考虑通过单击左侧投票箭头下方的复选标记(✔️)来接受它。应将颜色更改为绿色。我真的很感激。谢谢!
【解决方案2】:

你的错误是这样的:

FirebaseError:使用无效数据调用函数 Query.where()。不支持的字段值:未定义(在字段 teamId 中找到)

这意味着您在查询中为teamId 传递了无效的undefined 值,这是无效的。你应该调试你的代码并找出为什么 teamId 是未定义的,然后修复它。

【讨论】:

  • 我什至尝试对 id 进行硬编码以获取它不工作的数据
  • 这听起来与您第一次发布的问题不同。如果它本质上不同,也许会问另一个问题?
猜你喜欢
  • 1970-01-01
  • 2021-03-19
  • 2016-08-17
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2021-09-07
相关资源
最近更新 更多