我的firestore规则配置存在问题。
我有两个打字稿类cart.ts和item.ts
export class Item {
id?: string;
name: string;
quantity: string;
onCart: boolean;
position: number;
}
export class Cart {
id?: string;
name: string;
time: string;
buying: boolean;
bought: boolean;
boughtDate?: string;
personal: boolean;
userId: string;
items: Item[];
}
用户可以阅读所有公共购物车,只能阅读他的个人购物车(我想让公共购物车只能从您选择的人那里阅读,但现在我专注于更简单的事情。)
我的CartService上有一个方法来获取待处理的购物车:
export class CartService {
getPending(): Observable<Cart[]> {
return combineLatest(
this.angularFirestore
.collection(
'carts',
ref => ref
.where('personal', '==', true)
.where('userId', '==', this.authService.user.getValue().uid)
.where('bought', '==', false)
).snapshotChanges(),
this.angularFirestore
.collection(
'carts',
ref => ref
.where('bought', '==', false)
.where('personal', '==', false))
.snapshotChanges(),
).map(actions => {
let collections = [];
actions.forEach(action => collections.push(...action));
return collections
.map(action => {
const cart = action.payload.doc.data() as Cart;
return new Cart(cart.userId, cart, action.payload.doc.id)
});
});
}
}
我在我的firestore数据库上有这个规则:
service cloud.firestore {
match /databases/{database}/documents {
match /carts/{cartId} {
function isPersonal() {
return get(/databases/$(database)/documents/carts/$(cartId)).data.personal;
}
function canReadPublic() {
return !isPersonal() && request.auth != null;
}
function canReadPrivate() {
return isPersonal() && get(/databases/$(database)/documents/carts/$(cartId)).data.userId == request.auth.uid;
}
function canUserRead() {
return canReadPublic() || canReadPrivate();
}
allow read: if canUserRead();
match /{document=**} {
allow read: if canUserRead();
}
}
}
}
每次我调用此方法时都会抛出错误:
错误:权限丢失或不足。
我完全迷失了,我浪费了这么多时间,并且重写了所有这些代码十几次,我没有选择。