1

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 ?

1 Answer 1

4

Make sure you update your angularfire2 version to RC9 or higher - as this is when the PR was merged.

npm install angularfire2@latest

Add AngularFireFunctions to your providers in your app.module.

import { AngularFireFunctions } from 'angularfire2/functions';

@NgModule({
  providers: [
    AngularFireFunctions
  ],
});

Within your Component you can run httpsCallable function from AngularFireFunctions.

constructor(
  private afFun: AngularFireFunctions,
) { }

ngOnInit() {
  // Angular Fire - Converts Promise to Observable
  this.afFun.httpsCallable('myFunction')({ text: 'Some Request Data' })
    .pipe(first())
    .subscribe(resp => {
      console.log({ resp });
    }, err => {
      console.error({ err });
    });

  // Convert back to Promise
  const respRef = await this.afFun.httpsCallable('myFunction')({ text: 'Some Request Data' })
    .toPromise();

  console.log({ respRef });
}

A comment in the AngularFire2 repo has asked why the response has been converted to an Observable. The Firebase documentation has the function returning a Promise - so be aware of the inconsistency - it could change in the future.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.