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

React-Native调用Python版Firebase Cloud Functions遇错求助

问题分析与修复方案

一、sendHelloOnCall(Callable类型函数)错误原因及修复

Callable类型的Cloud Functions有特定的响应规范:不能返回https_fn.Response对象,必须直接返回JSON可序列化的数据(字符串、字典等),Firebase框架会自动封装成符合Callable要求的响应格式。

修复后的Python代码

@https_fn.on_call()
def sendHelloOnCall(req: https_fn.CallableRequest) -> str:
    return "HELLO"

错误根源

你返回的https_fn.Response("HELLO", status=200)不符合Callable函数的响应规范,导致服务端无法正确处理,进而引发Postman的INVALID_ARGUMENT错误和React Native的INTERNAL错误。


二、sendHello(onRequest类型函数)调用方式错误及修复

React Native的httpsCallable()方法是专门用于调用Callable类型的Cloud Functions,而sendHello是普通HTTP端点,必须用常规HTTP请求(如fetch)调用。

修复后的React Native代码

import functions from '@react-native-firebase/functions';

export function Home({navigation}) {
  // 正确调用Callable函数 sendHelloOnCall
  functions()
    .httpsCallable('sendHelloOnCall')()
    .then(r => {
      console.log(r.data); // Callable返回的结果在data字段中
    })
    .catch(error => {
      console.log(error);
    });

  // 正确调用onRequest函数 sendHello
  const sendHelloEndpoint = `https://<你的云函数区域>-<你的项目ID>.cloudfunctions.net/sendHello`;
  fetch(sendHelloEndpoint)
    .then(response => response.text())
    .then(text => {
      console.log(text);
    })
    .catch(error => {
      console.log(error);
    });

  return(
    <>
      <>
      </>
    </>
  );
}

错误根源

httpsCallable()会发送特定格式的请求(POST方法+JSON body),并期望响应符合Callable规范的JSON结构,但你的sendHello返回纯文本"HELLO",导致解析时触发格式错误。


验证建议

  1. 修复sendHelloOnCall后,用Postman测试:发送POST请求,Content-Type设为application/json,body传空对象{},应收到响应{"result":"HELLO"}。
  2. React Native中调用sendHelloOnCall时,需通过r.data获取返回值,而非直接打印r。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 01:42:25