angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Executing " “callable functions” using angularfire2

Open runtimeZero opened this issue 8 years ago • 10 comments

Callable functions in firebase can be executed as below:

firebase.functions().httpsCallable('addMessage'); I am wondering what is the equivalent of this in AngularFire2. I have scanned the documents and don't see any mention of this.

If there is no equivalent then how do i obtain a handle to underlying firebase object in AngularFire2 ?

runtimeZero avatar Jul 21 '18 13:07 runtimeZero

It looks like as of RC9 they added this ability through a new module called AngularFireFunctionsModule (I was literally just coming to this repo to look for this ability as well)

You can import that module with all the other angularfire ones and then use the AngularFireFunctions object exported from it in your component. There's no documentation but it looks like there's a method called httpsCallable just like in the firebase sdk. I assume it is just a wrapper around that same method.

atinybeardedman avatar Jul 23 '18 19:07 atinybeardedman

It seems to be working but it's not :)

constructor(public db: AngularFirestore, public functions: AngularFireFunctions) {} getData(id){ var getData = this.functions.httpsCallable('getData'); return getData({id: id}).then((result) => console.log(result)).catch((e)=> console.log(e)); } Doing this, sends the HTTPS request to the server but I have a "Response for preflight is invalid (redirect)" error then...

lenanex avatar Jul 27 '18 13:07 lenanex

We are getting docs on this soon!

davideast avatar Jul 30 '18 18:07 davideast

@lenanex , @atinybeardedman @runtimeZero try Angularfirebase's example : https://angularfirebase.com/lessons/saas-metered-subscriptions-with-stripe-billing-and-firebase/

There's an implementation that can help.

pradt avatar Aug 10 '18 05:08 pradt

@davideast Any idea why the angularfire2 wrapper converts the Promise returned from the native API to an Observable? A Promise seems to make more sense, which means any consumer needs to convert the Observable back to a Promise with Observable.toPromise, as is shown in the example posted by @pradt above.

madakk avatar Sep 02 '18 09:09 madakk

An update to @lenanex's code for anyone else that is having problems.

this.functions.httpsCallable('functionName')({ text: 'Some Argument' })
  .toPromise()
  .then(resp => {
    console.log({ resp });
   })
  .catch(err => {
    console.error({ err });
  });

sketchthat avatar Sep 04 '18 01:09 sketchthat

If you want a Promise, you can just use the vanilla Firebase API. No?

On Sun, Sep 2, 2018, 2:29 AM madakk [email protected] wrote:

@davideast https://github.com/davideast Any idea why the angularfire2 wrapper converts the Promise returned from the native API to an Observable? A Promise seems to make more sense, which means any consumer needs to convert the Observable back to a Promise with Observable.toPromise, as is shown in the example posted by @pradt https://github.com/pradt above.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/angular/angularfire2/issues/1789#issuecomment-417916872, or mute the thread https://github.com/notifications/unsubscribe-auth/AACvr7dTvKhs6cljeip9DiA8V8A3kU88ks5uW6TngaJpZM4VZn4M .

jamesdaniels avatar Sep 04 '18 04:09 jamesdaniels

Of course, but why have the wrapper convert it to an Observable if the API returns a Promise?

Observable indicates that the state might change after request - but it's always final.

sketchthat avatar Sep 04 '18 04:09 sketchthat

Not if it's a single. The idea is to allow it to be pipelined, including our own zone pipelining.

Observables are very much the first class citizen in Angular. That's the whole idea behind this library, else why use it at all?

On Mon, Sep 3, 2018, 9:50 PM Michael Kimpton [email protected] wrote:

Of course, but why have the wrapper convert it to an Observable if the API returns a Promise?

Observable indicates that the state might change after request - but it's always final.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/angular/angularfire2/issues/1789#issuecomment-418240393, or mute the thread https://github.com/notifications/unsubscribe-auth/AACvr0pII8vcmbWxjepQPJYtu8PgkyQOks5uXgajgaJpZM4VZn4M .

jamesdaniels avatar Sep 04 '18 07:09 jamesdaniels

Docs are up.

jamesdaniels avatar Sep 07 '18 05:09 jamesdaniels