首页 > 解决方案 > 从 Angular7 调用 Firebase 函数时出错:响应不是有效的 JSON 对象

问题描述

我尝试在角度应用程序中使用 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!');
    });

});
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 });
      });

  }
}

标签: angulargoogle-cloud-functions

解决方案


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

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

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


推荐阅读