未找到函数错误:名称:[get].在 Firestore 安全规则模拟中 [英] Function not found error: Name: [get]. in firestore security rules simulation

查看:26
本文介绍了未找到函数错误:名称:[get].在 Firestore 安全规则模拟中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

when i try to get data from UserProfile/{uId} using the above security rules it throws the following error in the firestore and in code it says insufficient permissions:

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

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

now the definition of above two function are here

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

第一个函数运行得很好但第二个没有

the first function runs very well but the second one doesn't

它抛出那个错误

据我所知,这个功能没问题

and as of my knowledge the function is alright

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

please help i have wasted a whole lot of time on this piddly problem

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

就是这样,请帮忙

推荐答案

更新:错误是规则模拟器中的一个错误,请参阅下面 Doug 的评论.

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

I tried out your rules and they worked as expected in the simulator.

规则:

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文档:

The request succeeds or fails based on the UserSettings/foo document in the database:

拒绝请求:

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

允许请求:

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

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

I think that errors can pop up when the data doesn't exist or isn't in the expected format.

例如,如果我删除了 /UserSettings/foo 文档,我也会收到:

For example, if I delete the /UserSettings/foo document I also receive:

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

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

I also get this error if the blocked field is anything other than a map (because in is a function for maps):

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

您可能可以通过使用 exists 并检查 blocked 的类型来清除这些错误,但无论哪种方式,您的规则仍应按预期拒绝请求.

You can probably clean up these errors by using exists and checking the type of blocked but either way, your rules should still deny the request as expected.

这篇关于未找到函数错误:名称:[get].在 Firestore 安全规则模拟中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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