在新创建的文档上使用 Get() 时 Firestore 规则失败 [英] Firestore Rule Failing While Using Get() on Newly Created Documents

查看:17
本文介绍了在新创建的文档上使用 Get() 时 Firestore 规则失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Firestore 规则中对新创建的文档使用 get() 会导致返回值为 false.如果您等待几秒钟并遇到在同一个新文档上调用 get() 的安全规则,则 get() 将返回预期值.我是否遗漏了规则和/或代码中的某些内容,或者这是 Firestore 的错误?

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);
}

推荐答案

此错误已解决.

不幸的是,这是目前已知的问题.我们正在修复,一旦解决将在此处更新.感谢和抱歉!

This is unfortunately a known issue at the moment. We're working on a fix and will update here once it's resolved. Thanks and sorry!

这篇关于在新创建的文档上使用 Get() 时 Firestore 规则失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆