“FirebaseError: internal” 当从客户端代码调用 onCall 云函数时

huangapple go评论64阅读模式
英文:

"FirebaseError: internal" when calling onCall cloud function from client code

问题

我有一个React Native - Expo应用程序,最近使用firebase-functions/v2/https的onCall()实现了一个基本的云函数,代码如下:

exports.addmessage = onCall((request) => {
    logger.info("Request: " + request.data.text);
    return { text: request.data.text };
});

我部署了这个函数,看起来部署似乎正确。然后,我尝试从我的客户端代码中的App组件调用这个函数,代码如下:

export default function App() {
  const addmessage = httpsCallable(functions, 'addmessage');
  
  function callFunction() {
    addmessage({ text: "Hello world!" })
      .then((result) => {
        console.log("Function called");
        console.log(result);
      }).catch((error) => {
        console.log("Function call failed");
        console.log(error);
      });
  }

  return (
    <SafeAreaProvider>
      <SafeAreaView style={styles.container}>
        <Button title="Call cloud function" onPress={() => callFunction()}></Button>
        <StatusBar style="auto" />
      </SafeAreaView>
    </SafeAreaProvider>
  );
}

当我点击按钮调用云函数时,我得到了"Function call failed"和"[FirebaseError: internal]"的消息。这是否是调用可调用函数的正确方式?
我已经检查了firebase控制台中函数的日志记录,但没有错误记录在那里。我应该如何找到/修复这个问题?
我已经测试了onRequest函数,它按预期工作。

我在项目的package.json中使用"firebase": "9.22.1"作为依赖项(尽管我也测试过"firebase": "9.20.0")。
在我的functions的package.json中,我有以下依赖项:"firebase-admin": "^11.8.0","firebase-functions": "^4.3.1"。

英文:

I have a React Native - Expo application, and I recently implemented a basic cloud function using the firebase-functions/v2/https onCall() like so:

exports.addmessage = onCall((request) =&gt; {
    logger.info(&quot;Request: &quot; + request.data.text);
    return {text: request.data.text}
});

I deploy this function, and it gets deployed seemingly correct. Then I try to call this function from my client code in the App - component like so:

export default function App() {
  const addmessage = httpsCallable(functions, &#39;addmessage&#39;);
  
  function callFunction() {
    addmessage({ text: &quot;Hello world!&quot; })
      .then((result) =&gt; {
        console.log(&quot;Function called&quot;);
        console.log(result);
      }).catch((error) =&gt; {
        console.log(&quot;Function call failed&quot;);
        console.log(error)
      });
  }

  return (
    &lt;SafeAreaProvider&gt;
      &lt;SafeAreaView style={styles.container}&gt;
        &lt;Button title=&quot;Call cloud function&quot; onPress={() =&gt; callFunction()}&gt;&lt;/Button&gt;
        &lt;StatusBar style=&quot;auto&quot; /&gt;
      &lt;/SafeAreaView&gt;
    &lt;/SafeAreaProvider&gt;
  );
}

When I click the button to call the cloud function, I get "Function call failed" and "[FirebaseError: internal]". Is this the correct way to call the callable function?
I have checked the logger for the function inside the firebase portal, but no errors are logged there. What can I do to find/fix this issue?
I have tested with an onRequest-function, and this works as expected.

I use "firebase": "9.22.1" as dependency in project-package.json (though I have also tested with "firebase": "9.20.0")
In my functions package.json, I have these dependencies: "firebase-admin": "^11.8.0", "firebase-functions": "^4.3.1".

答案1

得分: 1

I didn't try your Cloud Function but with

```javascript
exports.addmessage = onCall((request) => {
    logger.info("Request: " + request.data.text);
    return {text: text}
});

you actually don't declare the text variable.

The following should do the trick:

exports.addmessage = onCall((request) => {
    const text = request.data.text;
    logger.info("Request: " + text);
    return {text: text}   // Or return { text }
});

I also advise to better manage the error logging in your Cloud Function, by following the documentation: In the CF and in the client.


<details>
<summary>英文:</summary>

I didn&#39;t try your Cloud Function but with

```javascript
exports.addmessage = onCall((request) =&gt; {
    logger.info(&quot;Request: &quot; + request.data.text);
    return {text: text}
});

you actually don't declare the text variable.

The following should do the trick:

exports.addmessage = onCall((request) =&gt; {
    const text = request.data.text;
    logger.info(&quot;Request: &quot; + text);
    return {text: text}   // Or return { text }
});

I also advise to better manage the error logging in your Cloud Function, by following the documentation: In the CF and in the client.

huangapple
  • 本文由 发表于 2023年6月1日 15:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定