【问题标题】:AngularFireFunction Directly Call Cloud FunctionAngularFireFunction 直接调用云函数
【发布时间】:2020-11-16 19:21:51
【问题描述】:

我有一个 Angular 网站,我正在尝试将云功能集成为 Expressjs。只有云功能可以正常工作。当我访问 http://localhost:5001/project_id/us-central1/app 时,我可以看到控制台输出。但是对于 Angular,它不起作用。

这是尝试使用 angular 时的输出:

hosting: 127.0.0.1 - - [16/Nov/2020:18:57:01 +0000] "GET /favicon.ico HTTP/1.1" 200 4286 "http://localhost:5000/payment" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"

函数/index.js:

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

admin.initializeApp(functions.config().firebase);

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    console.log("Hello");
});
exports.app = functions.https.onRequest(app);

app.module.ts:

...
providers: [UserService, AngularFireAuthGuard,
    { provide: ORIGIN,useValue: 'http://localhost:5001' },
    { provide: REGION, useValue: 'us-central1' }],
...

firebase.json:

{
  "hosting": {
    "public": "dist/user",
    "ignore": [
      "firebase.json",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app",
        "destination": "/index.html"
      }
    ]
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "source": "functions"
  }
}

payment.component.ts(我尝试调用云函数):

constructor(private _fb: FormBuilder, private router: Router, private fns: AngularFireFunctions) {
    const callable = fns.httpsCallable('app');
    var data = callable({ name: 'some-data' });
    data.subscribe(async res => {
      console.log(res);
      });
  }

【问题讨论】:

  • 您还没有从 index.js 中导出任何内容。我建议查看文档。 firebase.google.com/docs/functions/…
  • @DougStevenson 我导出了,但我错过了那部分,抱歉。我编辑了第一篇文章。

标签: angular firebase google-cloud-functions


【解决方案1】:

您的客户端应用程序正在尝试调用可调用函数,但您的实际函数是由 express 应用程序支持的 HTTP 函数。这种组合是行不通的。如果要使用可调用函数,则必须按照documentation for callables 中的说明声明onCall 函数,而不是onRequestonRequest 用于正常的 HTTP 调用。从该文档中,请注意:

请务必记住,HTTPS 可调用函数是 类似于但不等同于 HTTP 函数。使用 HTTPS 可调用 您必须将客户端 SDK 与您的平台一起使用的功能 functions.https 后端 API(或实现协议)。可调用对象 与 HTTP 函数有以下主要区别:

或者,如果您确实想在后端使用 express 应用,您将无法使用 Firebase Functions 客户端 SDK 调用它。为此使用普通的 HTTP 客户端库。

【讨论】:

  • 谢谢。我对此很陌生。我更改为onCall,但它是一样的。我该怎么办?
  • 阅读并点击我在答案中提供的文档的链接。
猜你喜欢
  • 1970-01-01
  • 2020-11-13
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多