Skip to content

Calling AngularFireFunctions.httpsCallable results to error 404 because #1933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jenersonj opened this issue Oct 24, 2018 · 19 comments
Closed

Comments

@jenersonj
Copy link

Version info

Angular: 6.0

Firebase: 5.5.5

AngularFire: 5.1.0

How to reproduce these conditions

I created a service that aims to generate token from 3rd party service and used AngularFireFunctions to call a cloud function.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AngularFireFunctions } from '@angular/fire/functions';

import { CreditCard } from '@core/models';

@Injectable()
export class MagpieTokenService {
  private _generateToken;
  
  constructor(
    private http: HttpClient,
    private cloudFunctions: AngularFireFunctions
  ) { }

  generate(creditCard: CreditCard): Observable<any> {
    return this.generateToken(creditCard.generateFormValue())
  }

  protected get generateToken() {
    if(this._generateToken == null)
      this._generateToken = this.cloudFunctions.httpsCallable('generateToken');
    return this._generateToken;
  }
}

Now when I try to use the service in my component, error 404 was raised and I noticed that it called
https://null-rik-app.cloudfunctions.net/generateToken.

@achendrick
Copy link

The issue seems to come from #1920 (this file) resolving #1874.

To get around it for now, you can do this in your app.module.ts:

providers: [
  { provide: FunctionsRegionToken, useValue: 'us-central1' }
]

@george43g
Copy link
Contributor

Am also experiencing the same issue. It seems to be that it's not setting the region properly (even if left as default) and it's showing 'null' in the URL.

@predorock
Copy link

Same problem for me.

Angular CLI: 6.0.8
Node: 8.11.3
OS: darwin x64
Angular: 6.0.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cdk 6.4.7
@angular/cli 6.0.8
@angular/fire 5.1.0
@angular/material 6.4.7
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.2
typescript 2.7.2
webpack 4.8.3

@karlmnz
Copy link

karlmnz commented Nov 5, 2018

same here ..., getting same as george43g with the url = https://null-mysite-dev.cloudfunctions.net/somefunc

achendrick solution worked for me (but needs angularfire fix)

@jenersonj
Copy link
Author

Thanks @achendrick , it works.

@alberthoekstra
Copy link

Are you not getting CORS errors? I do, while localhost is added in the authorised domain list in the firebase console.

I'm testing an Ionic 3 app local (localhost:8100)

@sami6002
Copy link

sami6002 commented Nov 5, 2018

how to resolve the CORS errors? get firestore data doesn't happen, but function

@alberthoekstra
Copy link

Don't know yet.

@alberthoekstra
Copy link

I resolved it by adding CORS in the functions.

It is documented right here. Link

@sami6002
Copy link

@alberthoekstra can you tell me more detail? which file I can change? I can not found the functions.https or express. do I need to install firebase-functions and cors? btw, I use the ngx-admin. thanks so much.
my code just like this https://github.com/angular/angularfire2/blob/master/docs/functions/functions.md

@alberthoekstra
Copy link

alberthoekstra commented Nov 11, 2018

Sure no problem !
In my case the problem wasnt in the frontend code but in the functions code.

Check this example of Firebase where they created one function that return a CORS response.

In my case I added the CORS package. Then I added this code at the top of the code:

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

Then wrapped the original response in this code.

return cors(req, res, () => {
 // enter existing code here
 });

Hopefully this makes any sense :-)

@smtapp-smtio-cloud
Copy link

"AngularFireFunctions is super easy. You create a function on the server side and then "call" it by its name with the client library. This is all I find in terms of documentation for httpsCallable. "

Wondering if there is any other useful documentation than this available for this function.
I would like to make a POST request with multipart/form-data to a cloud function. How do I do this??

@M-Blobby
Copy link

I would like to make a POST request with multipart/form-data to a cloud function. How do I do this??

Same question, how to specify POST method ?

@alberthoekstra
Copy link

Maybe the docs can answer those?

@M-Blobby
Copy link

M-Blobby commented Dec 11, 2018

Sorry, I mean with the client side. My firebase functions is deployed and works well, but I'm searching how to call it from my application with angularfire2:
this.fns.httpsCallable('createNewUser')({ email: email, password: password }) .subscribe(res => { console.log('add user', res); });

From the chrometools I see my request has been send with "OPTIONS" method and return status 400. I'm not familiar with CORS, I'm reading docs about this but if you have any clue ...

EDIT: My problem was cors misunderstood. Your previous comment helped me. Thank you.

@jamesdaniels
Copy link
Member

Issue was fixed in the latest patch. Closing this issue but feel free to continue discussion RE CORS and such here.

@alex88
Copy link

alex88 commented Mar 18, 2019

@jamesdaniels how is this fixed? I've the latest version but with a simple

import * as functions from 'firebase-functions';
export const loadLists = functions.https.onCall((data, context) => {
  return context.auth;
});

and

this.fns.httpsCallable('loadLists')({}).subscribe(response => {
  console.log(response);
});

I still get the OPTIONS request and the CORS error

@mohamedaboelmagd
Copy link

The issue seems to come from #1920 (this file) resolving #1874.

To get around it for now, you can do this in your app.module.ts:

providers: [
  { provide: FunctionsRegionToken, useValue: 'us-central1' }
]

for latest version which compatible with angular 8

providers: [
   { provide: FUNCTIONS_ORIGIN, useValue: 'http://localhost:5005' }
  ]

import : [
AngularFireFunctionsModule
]

@francot
Copy link

francot commented Jun 23, 2020

Hi,
with:
Angular: 8.2.14
angular/fire: 6.0.0

I cannot call cloud functions hosted in region different from 'us-central1'.
I try @mohamedaboelmagd solution

providers: [
   { provide: FUNCTIONS_ORIGIN, useValue: 'http://localhost:5005' }
  ]

import : [
AngularFireFunctionsModule
]

But I don't understand where import FUNCTIONS_ORIGIN and why set useValue to 'http://localhost:5005'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests