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

Firestore Admin SDK 持续返回‘No QuerySnapshot Result’错误求助

Firestore查询抛出"No QuerySnapshot Result"错误排查

问题说明

对Firebase Firestore的diary集合执行查询时,明明存在匹配userID和指定时间范围的文档,却始终抛出No QuerySnapshot result错误。相关代码及错误信息如下:

业务代码

try {
  const diaryReference = firestore.collection("diary");
  const userEntriesSnapshot = await diaryReference
    .where("userID", "==", user.id)
    .where("time", ">=", startOfDayTimestamp)
    .where("time", "<=", endOfDayTimestamp)
    .get();

  if (userEntriesSnapshot.empty) {
    usersToNotify.push(user.fcmToken);
  } else {
    console.log(`✅ Found entries for ${user.id} today.`);
  }
} catch (error) {
  console.log("❌ Firebase Query Snapshot Error", error);
  console.log(user.fcmToken);
  usersToNotify.push(user.fcmToken);
}

错误栈

Error 774 |      *  transaction, or timestamp to use as read time.
775 |      */
776 |     async _get(transactionOrReadTime) {
777 |         const result = await this._getResponse(transactionOrReadTime);
778 |         if (!result.result) {
779 |             throw new Error('No QuerySnapshot result');
                        ^
error: No QuerySnapshot result
      at /Users/rohanrocks/VSCode/InsightfullApi/node_modules/@google-cloud/firestore/build/src/reference/query.js:779:19

解决方向

  • 升级依赖包:这个错误多出现于旧版@google-cloud/firestore或firebase-admin,直接升级到最新稳定版即可:
    npm update @google-cloud/firestore
    # 若使用firebase-admin则执行
    npm update firebase-admin
    
  • 校验时间戳类型:确保startOfDayTimestamp和endOfDayTimestamp是Firestore认可的Timestamp类型,而非普通JS Date或数字。可以用admin.firestore.Timestamp.fromDate(new Date())转换后再传入,避免类型不匹配导致查询异常。
  • 检查权限配置:确认服务账号拥有datastore.query权限,同时检查Firestore安全规则是否允许该查询。权限限制可能导致查询无结果返回,触发内部错误。
  • 排查查询上下文:如果代码中隐式传递了无效的事务或读时间参数,会导致_getResponse返回空结果。检查是否在其他逻辑中修改了查询上下文,或误传了非法参数。
  • 控制台验证查询:在Firebase控制台的Firestore数据面板手动执行相同条件的查询,确认是否真的存在匹配文档。如果控制台能查到,说明代码层面存在参数或依赖问题;如果控制台也查不到,检查文档的userID和time字段是否符合条件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 08:30:12