首页 > 解决方案 > Firebase https 函数在调用时返回“内部错误”

问题描述

我正在尝试从我的代码中调用 https firebase 函数。问题是,当调用我的函数时返回带有代码internal的错误,错误消息也是 internal. 我试图在网上搜索这个问题,我在描述这个错误代码的 firebase 文档上发现的一件事是有问题。所以我很困惑。这就是函数的调用方式:

export const getPlanDetails = (plan: string) => async (dispatch: Dispatch) => {
  try {
  
    dispatch({
      type: ERROR,
      payload: null,
    });
    dispatch({
      type: IS_FETCHING,
      payload: true,
    });
    const request = functions.httpsCallable("getPlanDetails");
    const response = await request({ plan: plan });

    dispatch({
      type: GET_PLAN_DETAIL,
      payload: {
        currency: response.data["currency"],
        name: response.data["name"],
        interval: response.data["interval"],
        amount: response.data["amount"],
      },
    });

  } catch (error) {
    console.log(error);
    dispatch({
      type: ERROR,
      payload: "An error occured getting the plan details",
    });
  } finally {
    dispatch({
      type: IS_FETCHING,
      payload: false,
    });
  }
};

这是返回的错误消息的屏幕: 在此处输入图像描述

我可以确认问题不是来自我的函数,因为当我在另一个项目上调用它时它工作得很好。因此,我还检查了该函数是否可以从我的 firebase 控制台上传到谷歌。它也不能是我的功能失败,因为我在它发生时设置了自定义错误消息。

这是功能代码:

exports.getPlanDetails = functions.https.onCall(async (data, context) => {
  try {
    const plan = await stripe.plans.retrieve(data.plan);
    return {
      currency: plan["currency"],
      name: plan["nickname"],
      interval: plan["interval"],
      amount: plan["amount"],
    };
  } catch (error) {
    console.log(error);
    await reportError(error, { user: context.auth.uid });
    throw new functions.https.HttpsError("something went wrong ...", error);
  }
});

这就是我初始化functionsvar 的方式:

import * as firebase from "firebase";
import { store } from "../redux";
import { AUTH_STATE_CHANGE } from "../redux/types/auth.type";

const firebaseConfig = {
//secret
};

firebase.initializeApp(firebaseConfig);
firebase.analytics();

const auth = firebase.auth();
const firestore = firebase.firestore();
const functions = firebase.functions();


export { auth, firestore, functions };

我认为 firebase 已经很好地初始化了,因为 auth 或 firestore 等其他服务运行良好

谢谢您的帮助

标签: node.jsfirebasegoogle-cloud-functions

解决方案


推荐阅读