【问题标题】:Deleting and updating docs for firestore security rules in local emulator在本地模拟器中删除和更新 Firestore 安全规则的文档
【发布时间】:2021-12-30 06:15:14
【问题描述】:

使用本地模拟器,我正在尝试为我的删除和更新测试一条规则,如下所示:

match /posts/{postId} {
  allow read: if true;
  allow create: if true;
  allow delete, update: if (resource.data.user.uid == request.auth.uid)
}

在我的测试文件中,我写了我正在尝试执行的现有文档的确切位置的路径,例如删除:

  it("If creator, Allow delete/update items in the post/comment collection", async () => {
    const db = firebase
      .initializeTestApp({ projectId: MY_PROJECT_ID, auth: myAuth })
      .firestore();
    const testDoc = db
      .collection("general")
      .doc("spaces")
      .collection("spaces")
      .doc("9LbUetZxWeL1ln5hv9ug")
      .collection("posts")
      .doc("RtUnezdAgXUfWmRSPpkF");
    await firebase.assertSucceeds(testDoc.delete());
  });

但是当我运行这个测试时,它给了我这个错误。但是,我知道这个文档存在,我希望在我的资源中获取的 user.uid 也是如此。我尝试使用此模拟器的方式有问题吗?

我还看到,如果我进入我的本地模拟器 firestore 数据库,我可以对文件进行硬编码,特别是调用 route my test 到它,然后它就可以工作了。

【问题讨论】:

  • 检查您是否连接到正确的项目。您说数据存在并且具有user.uid 属性,但来自数据库的响应说没有属性user

标签: javascript firebase react-native firebase-security


【解决方案1】:

运行测试时似乎没有用户登录。

由于您的安全规则检查request.auth.uid,当request.auth 为空时,它们会拒绝该操作。在运行测试之前检查myAuth.currentUser.uid,以确保它有一个值。

【讨论】:

  • 我有另一个测试,我测试文档 id 是否是通过测试的 request.auth.uid 的。所以我认为问题出在 resource.data.user.uid 上。
  • 一个?我有,我链接的firestore与模拟器firestore的不同吗?我是否需要在我的模拟器 Firestore 中构建一个与原始数据库类似的数据库?
  • "我认为问题出在 resource.data.user.uid" 鉴于错误消息,您的安全规则不同意该声明。当然,我们可能会在问题中遗漏上下文,但我发现最好明确指出我的代码中有一个当前用户,这样就不用争论了。
猜你喜欢
  • 2019-02-01
  • 2020-06-19
  • 2020-06-18
  • 2018-03-17
  • 2018-03-27
  • 1970-01-01
  • 2019-01-09
  • 2019-03-03
相关资源
最近更新 更多