Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error calling Firebase Functions from Angular7: Response is not valid JSON object

I am try use firebase functions in angular application. I use angularfire2 library In result:

{err: Error: Response is not valid JSON object. at new 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 });
      });

  }
}

like image 300
Sergey Konkov Avatar asked Apr 03 '19 20:04

Sergey Konkov


People also ask

Does Firebase functions support Node 16?

The engines field is required; it must specify one of the supported Node. js versions in order for you to deploy and run functions. Currently firebase init functions sets this field to 16 .

What is the difference between onCall http callable and onRequest HTTP request functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


1 Answers

This is how I was able to fix it.

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

It's weird that we need to do it, but it works!

like image 86
Freddy Avatar answered Oct 17 '22 08:10

Freddy