【发布时间】:2018-12-03 21:28:07
【问题描述】:
在the documentation 中声明您可以通过将字符串与另一个字符串值进行比较来进行查询,例如:
citiesRef.where('name', '>=', 'San Francisco');
citiesRef.where('state', '>=', 'CA').where('state', '<=', 'IN');
然后是roles part in the documentation,它展示了如何在 Firestore 文档中应用角色。但是没有显示如何查询这个..但如上例所示,这应该如下所示:
citiesRef.where(`roles.${user.uid}`, '>', '');
所以上面的这个查询应该返回所有值大于空字符串的文档,对吧?
在我的代码中,我有一个包含一个文档的组织集合:
{
"name": "MyAmazingCompany",
"roles": {
"my-user-uid": "owner"
}
}
如果我尝试查询我担任此类角色的组织:
organizationsRef.where(`roles.${user.uid}`, '>', '');
我将在浏览器控制台中获得 Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.(使用 firebase npm 包版本 5.1.0 并尝试过 5.0.3)。
只是为了确保我应该有权访问该文档,以下查询经过测试,它可以正常工作并返回该组织。
organizationsRef.where(`roles.${user.uid}`, '==', 'owner');
那么有什么问题呢?
还有人声称它应该可以工作:Firestore select where is not null
这是我的规则:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
function getRole(rsc) {
// Read from the "roles" map in the resource (rsc).
return rsc.data.roles[request.auth.uid];
}
function isOneOfRoles(rsc, array) {
// Determine if the user is one of an array of roles
return isSignedIn() && (getRole(rsc) in array);
}
match /organizations/{organizationId} {
allow read: if isOneOfRoles(resource, ['owner']);
allow write: if isOneOfRoles(resource, ['owner']);
}
}
}
就像我说的,如果我比较角色是否是所有者,它会起作用,但是如果用户的 uid 存在于角色数组中,我想得到结果,无论她担任什么角色。
【问题讨论】:
标签: javascript firebase google-cloud-firestore