【问题标题】:AngularFire firebase cloud function return value problemAngularFire firebase 云函数返回值问题
【发布时间】:2020-05-16 09:14:31
【问题描述】:

我对 AngularFire 有疑问,因为在他们的 github (Documentation) 中它说它返回一个 Observable。

这是 stackoverflow 上的一个新帐户,所以我无法在此处嵌入图像。

code problem

但问题是我无法订阅 Observable。

我的猜测是它必须与

(data: any) => Observable<any>;

因为当我尝试将 httpCallReleaseUser 变量类型定义为 Observable 时。我收到一个错误,告诉我它没有订阅 + 一些其他方法。

now with type definition

如果这就是问题

有什么区别

Observable<any>

(data: any) => Observable<any>;

【问题讨论】:

  • 你会想复习一下 ES6 然后解决这个问题
  • 为什么不直接告诉我正确答案呢?我以前见过这些 lambda 函数,但为什么 TYPE 在箭头的右侧?
  • (a) => b 在 ES6 中与 function(a){ b } 相同
  • 所以在这种特定情况下,它会是函数(数据){ Observable },这有什么意义

标签: angular typescript firebase observable angularfire


【解决方案1】:

你的定义是错误的。正如@Nelles 所指出的,您正在执行一项功能,但这并不能满足您的要求。您可能想要一些简单的东西,例如: data:Observable&lt;any&gt;; 表示您的变量是可观察类型。

您可能需要获取不同格式的数据,甚至可以使用接口。可能类似于以下内容。

export class foo {
    private data:Observable<myInterface[]>;

    constructor(
        @Inject(AngularFireDatabase) protected AfDb: AngularFireDatabase,
    ) { }

    public get():Observable<myInterface[]> // Return an array of myInterface items as an observable. Note that the data in firebase needs to match myInterface, but fields will be supported automatically
    {
        data = this.AfDb.list<myInterface[]>('firebasetable').valueChanges()
        return data;
    }
}

【讨论】:

    【解决方案2】:

    Observable() =&gt; Observable 的区别在于后者是一个输出Observable 的函数。

    AngularFireFunctions 具有签名httpsCallable(name:string) =&gt; (data:any) =&gt; Observable&lt;any&gt;,这意味着您必须调用它两次,首先使用您的函数名称,然后再次使用您要传递给函数的数据。

    functions.httpsCallable('fn-name')({ ... your data here or empty })

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2020-01-02
      • 2021-04-03
      • 2019-08-21
      • 2020-03-31
      • 2019-12-17
      • 2021-03-04
      相关资源
      最近更新 更多