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

Node.js中Firebase Realtime Database报错:db._checkNotDeleted is not a function

问题:Firebase Realtime Database 报错 db._checkNotDeleted is not a function

Node.js项目使用Firebase Realtime Database实现客户与治疗师匹配功能时,触发匹配路由后抛出错误:

Error during matching process: TypeError: db._checkNotDeleted is not a function
    at ref (C:\Users\USER\Documents\GitHub\backend\node_modules\@firebase\database\dist\index.node.cjs.js:12964:8)
    at matchClientsWithTherapists (C:\Users\USER\Documents\GitHub\backend\utils\match.js:55:47)

环境信息:

  • Firebase SDK 10.7.2
  • Node.js 18.17.0
  • 相同的Firebase配置在register.js中可正常运行

错误原因分析

报错指向match.js第55行的ref(responsesRef, clientId)调用,核心问题是错误地将已存在的Reference对象(responsesRef)作为ref函数的第一个参数传入。

Firebase ref函数的规则:

  • 第一个参数必须是**getDatabase()返回的数据库实例**,或是一个父级Reference对象
  • 当传入父级Reference时,内部会验证对象类型,但你在循环中的调用逻辑,错误地把responsesRef当成数据库实例使用,导致内部尝试调用仅数据库实例才有的_checkNotDeleted方法,而Reference对象无此方法,最终抛出类型错误。

对比register.js的正确用法:它始终以db(数据库实例)作为ref的第一个参数,或基于父级Reference调用child方法,因此未触发该问题。


修复方案

两种可行修复方式,任选其一即可:

方式1:基于数据库实例构建完整路径

修改match.js循环中的ref调用,直接使用db作为第一个参数,拼接完整路径:

// 原错误代码
const clientResponsesSnapshot = await get(ref(responsesRef, clientId));
const therapistResponsesSnapshot = await get(ref(responsesRef, therapistId));

// 修改后
const clientResponsesSnapshot = await get(ref(db, `responses/${clientId}`));
const therapistResponsesSnapshot = await get(ref(db, `responses/${therapistId}`));

方式2:使用Reference对象的child()方法

利用已创建的responsesRef(父级Reference),调用child()方法获取子节点Reference:

// 原错误代码
const clientResponsesSnapshot = await get(ref(responsesRef, clientId));
const therapistResponsesSnapshot = await get(ref(responsesRef, therapistId));

// 修改后
const clientResponsesSnapshot = await get(responsesRef.child(clientId));
const therapistResponsesSnapshot = await get(responsesRef.child(therapistId));

额外性能优化建议

当前嵌套循环内多次调用get会产生大量串行请求,建议用Promise.all批量获取数据提升效率:

// 替换原嵌套循环逻辑
for (const clientId in clients) {
  const clientResponsesSnapshot = await get(ref(db, `responses/${clientId}`));
  const clientResponses = clientResponsesSnapshot.val();

  // 批量获取所有治疗师的响应数据
  const therapistPromises = Object.keys(therapists).map(therapistId => 
    get(ref(db, `responses/${therapistId}`)).then(snap => ({ therapistId, data: snap.val() }))
  );
  const therapistResponsesList = await Promise.all(therapistPromises);

  therapistResponsesList.forEach(({ therapistId, data: therapistResponses }) => {
    const score = calculateMatchScore(clientResponses, therapistResponses);
    potentialMatches.push({ clientId, therapistId, score });
  });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:45:54