【问题标题】:How to execute callable functions using AngularFire2如何使用 AngularFire2 执行可调用函数
【发布时间】:2018-12-29 13:46:57
【问题描述】:

firebase 中的可调用函数可以如下执行:

firebase.functions().httpsCallable('addMessage');

我想知道 AngularFire2 中的等价物是什么。我扫描了文件,没有看到任何提及。

如果没有等价物,那么我如何在 AngularFire2 中获取底层 firebase 对象的句柄?

【问题讨论】:

    标签: angular firebase angularfire


    【解决方案1】:

    确保将 angularfire2 版本更新为 RC9 或更高版本 - 因为这是合并 PR 的时间。

    npm install angularfire2@latest
    

    在您的app.module 中将AngularFireFunctions 添加到您的providers

    import { AngularFireFunctions } from 'angularfire2/functions';
    
    @NgModule({
      providers: [
        AngularFireFunctions
      ],
    });
    

    在您的组件中,您可以从AngularFireFunctions 运行httpsCallable 函数。

    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 });
    }
    

    AngularFire2 存储库中的 comment 询问为什么响应已转换为 ObservableFirebase documentation 具有返回 Promise 的函数 - 因此请注意不一致之处 - 将来可能会发生变化。

    【讨论】:

      猜你喜欢
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多