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

React中Firestore查询投影如何实现?select函数报错求助

解决Firestore中select函数报错的问题

你的错误是因为Firestore模块化SDK(v9+)中select函数依赖较新的版本,或者项目SDK版本过低导致该函数不存在。以下是具体解决步骤:

  • 升级Firebase SDK到最新版本
    旧版本的Firebase模块化SDK可能未包含select函数,运行命令升级:

    npm install firebase@latest
    # 或使用yarn
    yarn add firebase@latest
    
  • 确认导入语句正确
    确保select从firebase/firestore正确导入,你的现有导入语句是正确的,但可以再次核对:

    import { getDocs, collection, query, where, select } from "firebase/firestore";
    
  • 可选:使用FieldPath指定字段
    如果直接传入字符串仍有问题,可通过FieldPath对象指定字段:

    import { getDocs, collection, query, where, select, FieldPath } from "firebase/firestore";
    
    const userRef = collection(db, 'usertest')
    const q = query(userRef, where('address','==','Tobago'), select(new FieldPath("address")))
    const querySS = await getDocs(q)
    

如果你的项目使用的是Firestore v8(非模块化)版本,投影需要通过链式调用实现:

const querySS = await db.collection('usertest').where('address','==','Tobago').select('address').get();

内容的提问来源于stack exchange,提问作者D. Rattansingh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 08:06:04