我在服务中有一个简单的集合引用
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.itineraryCollection = firebase
.firestore()
.collection(`itinerary/${user.uid}/itineraryList`);
}
});
我正在调用此服务 OnInit
ngOnInit() {
this.loggedInUser = firebase.auth().currentUser;
this.dataSvc.getItineraries()
.get()
.then( itineraryListSnapshot => {
this.itineraries = [];
itineraryListSnapshot.forEach(snap => {
this.itineraries.push({
id: snap.id,
activities: snap.data().activities,
destination: snap.data().destination,
startDate: snap.data().startDate,
endDate: snap.data().endDate,
tripDetails: snap.data().tripDetails,
userId: snap.data().userId
});
});
});
// this.itineraries = this.dataSvc.getUserItinerary();
console.log('logged in user add itin page', this.itineraries);
}
但是我在页面初始化时不断收到以下错误:
vendor.js:49548 ERROR Error: Uncaught (in promise): FirebaseError: [code=permission-denied]: Missing or insufficient permissions.
FirebaseError: Missing or insufficient permissions.
at new FirestoreError (vendor.js:76086)
at JsonProtoSerializer.push../node_modules/@firebase/firestore/dist/index.cjs.js.JsonProtoSerializer.fromRpcStatus (vendor.js:81385)
at JsonProtoSerializer.push../node_modules/@firebase/firestore/dist/index.cjs.js.JsonProtoSerializer.fromWatchChange (vendor.js:81882)
at PersistentListenStream.push../node_modules/@firebase/firestore/dist/index.cjs.js.PersistentListenStream.onMessage (vendor.js:90087)
at vendor.js:90016
at vendor.js:90056
at vendor.js:83144
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2749)
at Object.onInvoke (vendor.js:51123)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2748)
at resolvePromise (polyfills.js:3189)
at resolvePromise (polyfills.js:3146)
at polyfills.js:3250
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781)
at Object.onInvokeTask (vendor.js:51114)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2780)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2553)
at drainMicroTaskQueue (polyfills.js:2959)
at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (polyfills.js:2860)
at ZoneTask.invoke (polyfills.js:2845)
我不确定我在 firebase 中的数据库规则应该是什么,但我尝试了一堆不同的规则:
match /itinerary/{userId} {
allow read;
allow write;
}
match /itinerary/{userId}/itineraryList {
allow read;
allow write;
}
任何帮助将不胜感激
将其添加到"数据库规则"选项卡中:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
尝试将规则更改为:
match /itinerary/{userId}/itineraryList/{doc} {
allow read, write: if true;
}
请注意,"true"的 if 条件仅用于测试,因为它允许每个人访问该资源。我只建议在这里测试规则是否有效。
将/{doc}
添加到匹配项 - 允许将规则应用于要保护的文档。
我通过将数据库的规则更改为允许任何人读取来解决此问题,但仅将创建、更改、删除和更新的方法留给连接的用户。注释部分用于以后的实现,您已经为每个登录的用户定义了管理员规则,因此您可以传递只有管理员用户才能更改的内容。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
allow create, update, delete, write: if request.auth != null;
//allow create, update, delete, write: if request.auth != null && request.auth.uid == userIdAdmin;
}
}
}
我遇到了">未定义"文档ID的此问题。我检查了我的消防店规则,它们似乎没有造成任何伤害。一个错别字做到了。
removeTask(boardId: string, task: Task) {
return this.db
.collection('boards')
.doc(booardId) // <-- here, it has to be "boardId" instead
.update({
tasks: firebase.firestore.FieldValue.arrayRemove(task),
})
.catch((err) => {
console.error(err); //TODO: handle that exception properly
});
}
我的消防规则是这样的:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow create: if requestMatchesUID();
allow update: if resourceMatchesUID() && requestMatchesUID();
allow delete: if resourceMatchesUID()
}
function requestMatchesUID(){
return request.auth.uid == request.resource.data.uid;
}
function resourceMatchesUID(){
return request.auth.uid == resource.data.uid;
}
}
}
有关规则,请查看消防船