我开始试用Firebase和Firestore并遇到问题。 每当我设置我的规则并尝试查询文档时,我都会收到一个权限被拒绝的错误,但我没有看到这一点,为什么它被拒绝...
以下是我的规则:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow write: if request.auth.uid == resource.data.uid;
allow read: if request.auth.uid != null;
allow create: if request.auth.uid != null;
}
function userMightRead() {
return (
resource.data.viewers[request.auth.uid] != null ||
resource.data.participants[request.auth.uid] != null ||
request.auth.uid == resource.data.owner
);
}
function userMightWrite() {
return (
resource.data.participants[request.auth.uid] != null ||
request.auth.uid == resource.data.owner
);
}
match /projects/{project} {
allow create: if request.auth.uid != null;
allow read: if userMightRead();
allow write: if userMightWrite();
}
}
}
我在JS中的查询:
db.collection('projects')
.where('viewer.' + this.user.uid, '==', true)
.onSnapshot(snapshot => {
let projects = [];
snapshot.forEach(projectDoc => {
projects.push({
tasks: [],
uid: projectDoc.id,
...projectDoc.data(),
ref: projectDoc,
});
});
this.projects.viewer = projects;
this.loaded = true;
}, (error) => {
console.error("Couldn't fetch 'viewer' Projects!", error);
alert(error.message);
});
“项目”集合中的当前数据:
name: 'StatisticTool',
owner: '4UJFZOgQExUlqpYAToScWEh2GsA3'
participants: {
4UJFZOgQExUlqpYAToScWEh2GsA3: true
},
viewers: {
4UJFZOgQExUlqpYAToScWEh2GsA3: true
},
编辑:尝试访问文档的用户具有UID:4UJFZOgQExUlqpYAToScWEh2GsA3