You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已认证用户执行Firebase创建操作仍遇权限不足问题求助

问题排查:Firestore创建操作权限错误(Missing or Insufficient Permissions)

错误原因分析

1. 提交数据包含未授权字段

你的Firestore规则中,recipeHasOnlyAllowedFields()函数明确限定创建/更新操作只能包含以下字段:name、category、directions、publishDate、ingredients。但你提交的数据中额外包含了isPublished: true字段,违反了keys.hasOnly(requiredFields)的校验规则,直接导致权限校验失败。

2. 时间戳字段类型不匹配(潜在触发点)

规则里isRecipeValid()要求publishDate必须是timestamp类型,但你提交的是字符串格式的"01/01/2023"。即使注释说明“pretend this is a timestamp”,如果实际代码未将其转换为Firestore原生的Timestamp对象,这个校验也会失败,进一步触发权限错误。

解决办法

1. 移除未授权字段

删除提交数据中的isPublished字段,确保仅包含规则允许的字段:

{
  name: "Bread",
  category: "bakedGoods",
  directions: "Mix the ingredients together",
  ingredients: ["3 cups of flour", "1 cup of water"],
  publishDate: // 此处需传入Firestore Timestamp类型值
}

2. 修正时间戳字段类型

将publishDate转换为Firestore支持的Timestamp类型,以JavaScript为例:

publishDate: firebase.firestore.Timestamp.fromDate(new Date("2023-01-01"))

3. 可选:调整规则允许新增字段(若业务需要)

如果确实需要保留isPublished字段,修改规则中的校验逻辑:

  • 更新recipeHasOnlyAllowedFields()的字段列表:
let requiredFields = [
    'name',
    'category',
    'directions',
    'publishDate',
    'ingredients',
    'isPublished' // 添加新字段
];
  • 在isRecipeValid()中补充字段类型校验:
return (
    data.name is string && data.name != '' &&
    data.category is string && data.category != '' &&
    data.directions is string && data.directions != '' &&
    data.publishDate is timestamp &&
    data.ingredients.size() > 0 &&
    data.isPublished is bool // 新增布尔类型校验
)

内容的提问来源于stack exchange,提问作者TheScrappyDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:15:04