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

Firebase Firestore v9访问customers集合时出现TypeError: n.indexOf is not a function错误

问题解决:访问Firestore customers集合报错TypeError及权限问题

一、先解决TypeError: n.indexOf is not a function的直接原因

你的查询代码存在语法错误,doc()是用来获取单个文档的API,不能和where()条件查询混用。正确的集合条件查询需要用query()方法包裹:

错误代码:

const taskQuery = doc(collection(db, "customers"), where("uid", "==", user.uid))

修正后的代码:

// 确保导入所需方法
import { collection, query, where, getDocs } from "firebase/firestore";

const taskQuery = query(collection(db, "customers"), where("uid", "==", user.uid))
const loadCheckout = async (priceId) => {
    try {
        const taskDocs = await getDocs(taskQuery)
        // 此处可添加文档数据处理逻辑
    } catch (error) {
        console.log(error.message)
    }
}

二、权限规则的问题排查与修复

你提到移除where后出现权限不足提示,原规则存在两个关键问题:

1. 规则嵌套错误

match /customers/{uid}的代码块未闭合,导致products的规则被错误嵌套在customers规则内部,会引发规则解析异常。

2. customers规则匹配范围问题

原规则match /customers/{uid}仅匹配文档ID等于用户uid的单个文档,但如果你的customers集合中是多个包含uid字段的文档,规则需要调整为匹配集合下所有文档,并校验文档内的uid字段。

修正后的权限规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // 匹配customers集合下所有文档
    match /customers/{docId} {
      allow read: if request.auth != null && resource.data.uid == request.auth.uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == resource.data.uid;
      }
      match /subscriptions/{id} {
        allow read: if request.auth.uid == resource.data.uid;
      }
    }

    // 独立的products集合规则
    match /products/{id} {
      allow read: if true;

      match /prices/{id} {
        allow read: if true;
      }

      match /tax_rates/{id} {
        allow read: if true;
      }
    }
  }
}

总结操作步骤

  1. 替换错误的查询代码,使用query()构建集合条件查询
  2. 修正Firestore权限规则的嵌套错误,调整customers的权限校验逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 18:06:40