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

Firebase查询无法对数字字段进行查询问题求助

解决Firebase数字字段查询失效问题

排查方向及解决方案

1. 字段名拼写不一致

你的数据结构里年份字段是year,但代码中查询的是jaar,这会导致完全匹配不到数据。修改代码中的字段名,保持和Firebase存储的字段一致:

export const getSpecialisationsForYear = async (year) => {
 const specialisationRef = collection(db, 'specialisaties');

 // 将'jaar'改为'year',匹配数据结构中的字段名
 const q = query(specialisationRef, where('year', '==', year));

 const querySnapshot = await getDocs(q);
 querySnapshot.forEach((doc) => {
    console.log(doc.id, ' => ', doc.data());
  });
};

2. 传入参数类型不匹配

如果传入的year是字符串类型(比如从表单输入、URL参数获取的"3"),而Firebase中存储的是Number类型的3,严格类型匹配会导致查询无结果。需要确保传入的参数是数字类型:

// 调用函数时转换为数字
getSpecialisationsForYear(Number(yearInput));

// 或者在函数内部处理类型转换
export const getSpecialisationsForYear = async (year) => {
 const specialisationRef = collection(db, 'specialisaties');
 // 转换为数字类型
 const numericYear = Number(year);
 const q = query(specialisationRef, where('year', '==', numericYear));

 const querySnapshot = await getDocs(q);
 querySnapshot.forEach((doc) => {
    console.log(doc.id, ' => ', doc.data());
  });
};

3. 验证Firebase数据存储类型

确认Firebase控制台中specialisaties集合下的文档,year字段确实是Number类型(不是字符串)。如果是字符串类型,要么修改数据类型为Number,要么在查询时用字符串匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 00:18:31