【问题标题】:error calling Firebase Functions from Angular7: Response is not valid JSON object从 Angular7 调用 Firebase 函数时出错:响应不是有效的 JSON 对象
【发布时间】:2019-08-25 12:07:34
【问题描述】:

我尝试在 Angular 应用程序中使用 firebase 函数。 我使用 angularfire2 库 结果:

{错误:错误:响应不是有效的 JSON 对象。 在新的 HttpsErrorImpl (http://localhost:4200/vendor.j...}

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

const cors = require('cors')({
    origin: true
  });

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

});
import { Component, OnInit } from '@angular/core';
import { AngularFireFunctions } from 'angularfire2/functions';
import { Observable } from 'rxjs';
import { first } from 'rxjs/operators';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

  message: Observable<string>;
  message2: string;

  constructor(private fns: AngularFireFunctions) {
  }

  ngOnInit() {
  }

  getfsf() {

    this.fns.httpsCallable('helloWorld')({ text: 'Some Request Data' })
      .pipe(first())
      .subscribe(resp => {
        console.log({ resp });
      }, err => {
        console.error({ err });
      });

  }
}

【问题讨论】:

  • 我面临着完全相同的问题。想看看有没有人解决。

标签: angular google-cloud-functions


【解决方案1】:

这就是我能够修复它的方法。

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

我们需要这样做很奇怪,但它确实有效!

【讨论】:

    【解决方案2】:

    对于那些其他答案不起作用的人,这对我有用,因为我使用 @angular/fire 库和 Firebase Hosting 来托管我的 Angular 应用程序:

    我必须在我的 firebase.json 文件中添加一个函数重写:

      "hosting": {
        "rewrites": [
          {
            "source": "/project-name/us-central1/someFunction",
            "function": "someFunction"
          },
          {
            "source": "/project-name/us-central1/anotherFunction",
            "function": "anotherFunction"
          },
          ...
        ]
      }
    

    更多细节可以在这里找到:https://github.com/angular/angularfire/blob/master/docs/functions/functions.md#firebase-hosting-integration

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2021-06-24
      • 2020-07-26
      • 2021-03-03
      • 1970-01-01
      • 2020-08-17
      • 2022-07-07
      • 1970-01-01
      • 2018-12-13
      相关资源
      最近更新 更多