找不到函数错误:名称:[get]。在 firestore 安全规则模拟中

问题描述 投票:0回答:2
match /UserProfile {
    match /{uId}{
    allow get: if isUserLoggedIn() && !isUserBlocked(uId);
  }

当我尝试使用上述安全规则从 UserProfile/{uId} 获取数据时,它会在 firestore 中抛出以下错误,并在代码中显示权限不足:

Error running simulation — Error: simulator.rules line [199], column [28]. Function not found error: Name: [get].

现在上面两个函数的定义都在这里了

function isUserLoggedIn(){
    return request.auth != null;
}

function isUserBlocked(uId){
    return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
}

第一个功能运行得很好 但第二个没有

它抛出该错误

据我所知,功能没问题

请帮助我在这个小问题上浪费了很多时间

我已经尝试过了

  1. 我在其中一个线程中读到这是一个暂时的问题 但事实并非如此。现在已经超过48小时了
  2. 有人还提到,这只是模拟器中的错误,但代码会顺利运行,即使情况并非如此。在代码中,错误是我所期望的权限不足
  3. 我已经正确阅读了文档,所以我的代码没问题,已经在其他规则中测试了 get 方法,并且它工作正常

就是这样,请帮忙

firebase google-cloud-firestore firebase-security
2个回答
8
投票

更新:这些错误是规则模拟器中的错误,请参阅下面 Doug 的评论。

我尝试了你的规则,它们在模拟器中按预期工作。

规则:

match /UserProfile {
  function isUserLoggedIn(){
    return request.auth != null;
  }

  function isUserBlocked(uId){
    return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
  }

  match /{uId}{
    allow get: if isUserLoggedIn() && !isUserBlocked(uId);
  }
}

在模拟器中测试查询:

get /UserProfile/foo
Authenticated: Yes
Firebase UID: bar

请求成功或失败根据数据库中的

UserSettings/foo
文档:

拒绝请求:

/UserSettings/foo    
{
 content: "my content"
 blocked: { bar: true }
}

允许请求:

/UserSettings/foo    
{
 content: "my content"
 blocked: { otheruser: true }
}

我认为当数据不存在或不符合预期格式时,可能会弹出错误。

例如,如果我删除

/UserSettings/foo
文档,我还会收到:

Error: simulator.rules line [58], column [28]. Function not found error: Name: [get].

如果

blocked
字段不是地图,我也会收到此错误(因为
in
是地图的函数):

Error: simulator.rules line [58], column [95]. Function not found error: Name: [in].

您可以通过使用

exists
并检查
blocked
的类型来清除这些错误,但无论哪种方式,您的规则仍应按预期拒绝请求。


0
投票

问题是您正在使用

$(database)
$(userId)
而不是仅
(default)
userId

return isAuth() && exists(/database/(default)/documents/users/request.auth.uid) && get(/databases/(default)/documents/users/request.auth.uid).data.admin == true; }

Firstore 不允许数据文档使用通配符,例如 ${database} 模拟器现在应该可以工作。这让我很恼火,直到我修复了它,也许在他们允许通配符之前,但最近它只为每个项目使用默认数据库。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.