Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - Firestore Rule Failing While Using Get() on Newly Created Documents

Using get() in Firestore rules on a newly created document causes the return value to be false. If you wait a few seconds and hit a security rule that calls get() on that same new document, get() will then return the expected value. Am I missing something in my rules and/or code, or is this a bug with Firestore?

service cloud.firestore {
  match /databases/{database}/documents {

    match /budgets/{budgetId} {

      allow read: if resource.data.userId == request.auth.uid;
      allow create: if request.auth.uid == request.resource.data.userId;

      match /accounts/{accountId} {
        allow read, create, update: if userOwnsBudget();  // <--- failing for newly created budget documents
      }

      function userOwnsBudget() {
        return get(/databases/$(database)/documents/budgets/$(budgetId)).data.userId == request.auth.uid;
      }
    }
  }
}
const data: Budget = {
    userId: userId,
    budgetName: budgetName,
    currencyType: currencyType
};

try {
    const newBudget = await this.afs.collection<Budget>('budgets').add(data);

    const accountsCollection: AngularFirestoreCollection<BudgetAccount> = this.afs.collection<BudgetAccount>('budgets/' + newBudget.id + '/accounts');

    //Insufficient permission, but occasionally succeeds 
    accountsCollection.valueChanges().subscribe(accounts => {
        console.log(accounts);
    });

    setTimeout(() => {
        accountsCollection.valueChanges().subscribe(accounts => {
            console.log(accounts)
        });
    }, someArbitaryTime) // Typically waiting 5 seconds is enough, but occasionally that will still fail 

} catch(error) {
    console.error(error);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...