【问题标题】:Calling firebase function results in Internal Error调用 firebase 函数导致内部错误
【发布时间】:2020-02-10 08:27:02
【问题描述】:

我正在从 Web 应用程序调用一个简单的 firebase 函数,但出现内部错误。有人可以建议我哪里出错了。

我看到了类似的问题,但他们没有回答我面临的问题。

我可以确认该功能已部署到 Firebase。

通过在浏览器中粘贴以下链接,我得到了回复。 https://us-central1-cureme-dac13.cloudfunctions.net/helloWorld

index.js 文件有代码(Firebase 云函数在 index.js 中定义)

const functions = require('firebase-functions');

exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("Hello from Firebase!");
});

webApp.js 具有以下代码(客户端/网站)

var messageA = firebase.functions().httpsCallable('helloWorld');

messageA().then(function(result) {

  console.log("resultFromFirebaseFunctionCall: "+result)

}).catch(function(error) {
  // Getting the Error details.
  var code      = error.code;
  var message   = error.message;
  var details   = error.details;
  // ...
  console.log("error.message: "+error.message+" error.code: "+error.code+" error.details: "+error.details)
  // Prints: error.message: INTERNAL error.code: internal error.details: undefined
});

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    您混淆了Callable Cloud FunctionsHTTPS Cloud Functions

    通过做

    exports.helloWorld = functions.https.onRequest(...)
    

    您定义一个 HTTPS 云函数,

    但是通过做

    var messageA = firebase.functions().httpsCallable('helloWorld');
    messageA().then(function(result) {...});
    

    在您的客户端/前端,您实际上调用了一个 Callable Cloud Function。


    您应该将您的云函数更改为可调用函数,或者通过向云函数 URL 发送 HTTP GET 请求来调用/调用 helloWorld HTTPS 云函数(类似于您在浏览器中通过“粘贴浏览器中的https://us-central1-cureme-dac13.cloudfunctions.net/helloWorld 链接”)。

    例如,通过使用Axios 库,您可以:

    axios.get('https://us-central1-cureme-dac13.cloudfunctions.net/helloWorld')
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      })
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2018-07-03
    • 2021-01-06
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多