从Angular7调用Firebase函数时出错:响应无效的JSON对象

Ser*_*kov 5 google-cloud-functions angular

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

{err:错误:响应不是有效的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!');
    });

});
Run Code Online (Sandbox Code Playgroud)
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 });
      });

  }
}

Run Code Online (Sandbox Code Playgroud)

Fre*_*ddy 2

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

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

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