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
相关产品推荐
相关产品推荐

