firebase - 调用 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
});
解决方案
您正在混淆Callable Cloud Functions和HTTPS 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);
})
推荐阅读
- c# - 捕获未更新,我可以单击图片框
- c# - 将多个参数从 c# 传递给 python
- sql - 在 SQL Server 中,如何获取每组参数的最新条目集?
- ios - iOS 应用的 Fabric 缺少 dsym
- xml - xsl:text 会发生什么?
- java - 从前端到 Java Spring Boot 后端的 CORS 请求出现错误 401
- regex - 正则表达式替换并在第一个零之前添加连字符
- vue.js - 带有 VueJS 的客户端 OAuth2 应用程序 - 需要服务器组件吗?
- javascript - javascript+browser 复制到剪贴板失败,数据超过 ~150k 使用 document.execCommand('copy')
- unicode - Common Lisp 中 unicode 字符的语法(如果有)是什么?