【发布时间】:2020-01-21 04:55:59
【问题描述】:
如何查询具有数字键集的数组中的值?
例如,这个查询不起作用,因为我相信它正在查看索引而不是值:
var db = firebase.firestore();
var users = db.collection("Users");
var currentUser = firebase.auth().currentUser;
var result = users.where('liked', 'array-contains', currentUser.uid);
if (result.length > 0) {
return 'You have a match!';
} else {
return 'No matches';
}
【问题讨论】:
-
array-contains运算符作用于数组中的值,而不是它们的索引。您可能希望在运行查询之前记录currentUser.uid的值,以查看它是否具有您期望的值。您也可以尝试使用硬编码字符串,以排除它是由 UID 查找引起的。 -
@FrankvanPuffelen 我实际上已经尝试了这两种方法,但它仍然没有工作,所以我认为它正在脱离索引。现在我明白这不是索引/值......它必须是我如何处理返回的数据。你能告诉我返回什么数据类型吗?文档只是说明
"This query returns every city document where the regions field is an array that contains west_coast. If the array has multiple instances of the value you query on, the document is included in the results only once."。它返回一个数组还是单个用户? -
根据 firebase.google.com/docs/reference/js/… ,您的
result变量属于“查询”类型,因此您应该.get()它,如此处所述 firebase.google.com/docs/reference/js/…
标签: javascript firebase react-native google-cloud-firestore react-native-firebase