【问题标题】:Calling a Firebase function in Angular在 Angular 中调用 Firebase 函数
【发布时间】:2018-11-15 20:35:36
【问题描述】:

我做了这个功能:

  getAllUserLikedProperties(): AngularFirestoreCollection<any> {
    return this.afs.collection('users', ref =>
      ref.where('uid', '==', this._auth.currentUserId)
        .where('likedProp', '==', true)
    );
  }

我试着这样称呼它:

  ngOnInit() {
      this._property.getAllUserLikedProperties().subscribe(likedProp => {
        console.log(likedProp);
        this.likedProp = likedProp;
      });

    }

但是它返回这个错误:

ERROR TypeError: this._property.getAllUserLikedProperties is not a function

有人知道为什么吗?知道我应该怎么称呼它吗?

【问题讨论】:

  • 什么是this._property
  • 什么是_property
  • @dfsq public _property: PropertyService(链接到我创建该函数的服务)
  • @sabithpocker 我已经在上面回答了,但无法标记你
  • 确保您的服务是可注入的。

标签: javascript angular typescript firebase


【解决方案1】:

您的服务函数getAllLikedProperties() 没有返回可观察对象,它返回的是您过滤后的 afs 集合的值。

改变你的函数返回一个可观察的,你可以订阅它,例如:

getAllUserLikedProperties(): AngularFirestoreCollection<any> {

    const properties$: Subject<any> = new Subject<any>();
    properties$.next(this.afs.collection('users', ref =>
      ref.where('uid', '==', this._auth.currentUserId)
        .where('likedProp', '==', true)
    ));
   return properties$;
}

或者,您可以使用 RxJs 的 of 运算符返回可观察流:

import { of } from 'rxjs';

getAllUserLikedProperties(): AngularFirestoreCollection<any> {
    return of(this.afs.collection('users', ref =>
      ref.where('uid', '==', this._auth.currentUserId)
        .where('likedProp', '==', true)
    ));
}

或者,完全忘记订阅,直接分配值。

ngOnInit() {
    this.likedProp = this._property.getAllUserLikedProperties();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 2017-02-05
    • 2021-04-08
    • 1970-01-01
    • 2021-08-13
    • 2017-12-26
    • 2021-10-14
    • 2020-11-24
    相关资源
    最近更新 更多