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

Firebase云函数报错Maximum call stack size exceeded求助

问题原因

你遇到的Maximum call stack size exceeded错误,核心原因是前端传递了完整的auth.currentUser对象给云函数。auth.currentUser包含复杂的内部属性(如ProactiveRefresh相关结构),这些属性存在循环引用,当Firebase的httpsCallable尝试序列化该对象时,会无限递归遍历属性,最终触发栈溢出。

解决方案

1. 前端代码修复

将传递的userId从完整用户对象改为用户ID字符串:

const testUpdateProfile = async (username: string) => {
    await saveUser({
        userId: auth.currentUser.uid, // 仅传递uid字符串,而非整个用户对象
        username: username,
    });
}

2. 云函数优化(更安全)

云函数可直接通过context.auth.uid获取当前登录用户ID,无需前端传递userId,既能避免参数篡改风险,也能简化代码:

exports.changeUserData = functions.region("europe-west1")
    .https.onCall((data, context) => {
      // 直接从context获取用户ID,无需前端传入
      const userId = context.auth.uid;
      const newData = {username: data.username};

      // 注意:模板字符串需用反引号,否则${userId}无法解析
      return admin.database().ref(`/users/${userId}`)
          .set(newData).then(() => {
            console.log(`user ${userId} updated`);
            return {username: data.username};
          }).catch((error) => {
            throw new functions.https.HttpsError(
                "aborted",
                `${error.code} ${error.message}`,
            );
          });
    });

额外防护

建议在调用函数前检查用户是否已登录,避免auth.currentUser为null的情况:

const testUpdateProfile = async (username: string) => {
    if (!auth.currentUser) {
        alert("请先登录");
        return;
    }
    await saveUser({
        userId: auth.currentUser.uid,
        username: username,
    });
}

内容的提问来源于stack exchange,提问作者Wesley van S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 04:35:39