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

Firebase Cloud Functions报错:Maximum call stack size exceeded 求助

解决Firebase Cloud Functions中“Maximum call stack size exceeded”错误

出现这个错误的核心原因是:你直接返回了数据库transaction操作的Promise对象,而Firebase的onCall函数要求返回可序列化的JSON数据,但transaction的返回结果包含循环引用的内部对象,导致序列化过程中栈溢出。

修复后的代码如下:

exports.categoryOpened = functions.https.onCall(async (data, context) => {
  const categoryUUID = data.categoryUUID;
  const countryCode = data.countryCode;

  // 获取当前日期和小时
  const date = new Date();
  const dd = String(date.getDate()).padStart(2, "0");
  const mm = String(date.getMonth() + 1).padStart(2, "0");
  const yyyy = date.getFullYear();
  const hour = date.getHours();

  const path = `analytics/${countryCode}/categories/${categoryUUID}/categoryOpened/${dd}-${mm}-${yyyy}/${hour}`;

  // 执行事务并获取结果
  const transactionResult = await admin.database(analyticsDb).ref(path).transaction((count) => {
    return count ? count + 1 : 1;
  });

  // 返回可序列化的简单JSON对象,避免循环引用
  return {
    success: true,
    newCount: transactionResult.snapshot.val()
  };
});

关键修复点:

  • 使用async/await等待事务执行完成,确保拿到最终结果后再返回
  • 不再直接返回transaction的Promise,而是提取快照中的计数数值,包装成无循环引用的普通JSON对象返回
  • 用ES6模板字符串简化路径拼接,提升代码可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 17:05:58