首页 > 解决方案 > 调用 firebase 函数导致内部错误

问题描述

我正在从 Web 应用程序调用一个简单的 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
});

标签: firebasegoogle-cloud-functions

解决方案


您正在混淆Callable Cloud FunctionsHTTPS Cloud Functions

通过做

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

您定义一个 HTTPS 云函数,

但通过做

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

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


您应该将云函数更改为可调用函数,或者helloWorld通过向云函数 URL 发送 HTTP GET 请求来调用/调用 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);
  })

推荐阅读