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

如何在Expo Go应用中调用Firebase可调用云函数?

在Expo Go中调用Firebase可调用函数的可行方案

当然有可行方案——直接使用Firebase官方Web SDK即可,不需要依赖rnfirebase(它需要原生配置,Expo Go不支持这类原生依赖操作)。具体步骤如下:

  • 安装Firebase Web SDK
    在项目根目录执行命令:

    npm install firebase
    # 或者用yarn
    yarn add firebase
    
  • 初始化Firebase配置
    新建一个firebaseConfig.js文件,填入你的项目配置(从Firebase控制台获取):

    import { initializeApp } from "firebase/app";
    import { getFunctions } from "firebase/functions";
    
    const firebaseConfig = {
      apiKey: "你的API_KEY",
      authDomain: "你的AUTH_DOMAIN",
      projectId: "你的PROJECT_ID",
      storageBucket: "你的STORAGE_BUCKET",
      messagingSenderId: "你的SENDER_ID",
      appId: "你的APP_ID"
    };
    
    const app = initializeApp(firebaseConfig);
    const functions = getFunctions(app);
    // 若函数部署在非默认区域(比如eu-west1),需指定区域:
    // const functions = getFunctions(app, 'eu-west1');
    
    export { functions };
    
  • 调用可调用函数
    在需要调用的组件里引入配置,执行函数调用:

    import { functions } from './firebaseConfig';
    import { httpsCallable } from "firebase/functions";
    
    const invokeMyFunction = async () => {
      try {
        const targetFunction = httpsCallable(functions, '你的函数名称');
        const response = await targetFunction({ /* 传递给函数的参数 */ });
        console.log('函数返回结果:', response.data);
      } catch (err) {
        console.error('调用失败:', err);
      }
    };
    

注意事项

  • 确保你的Firebase可调用函数已经通过firebase deploy --only functions部署到线上
  • 如果遇到跨域错误,检查函数部署区域是否和初始化时指定的一致,或在Firebase控制台确认函数配置
  • 这种方式完全兼容Expo Go,无需eject项目或构建自定义开发客户端

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 13:04:51