How to execute callable functions using AngularFire2

a 夏天 提交于 2019-12-11 15:14:34

问题


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:


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.



来源:https://stackoverflow.com/questions/51456237/how-to-execute-callable-functions-using-angularfire2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!