【问题标题】:Get value returned on Observable <any> in Angular Typescript (Firebase)在 Angular Typescript (Firebase) 中获取 Observable <any> 返回的值
【发布时间】:2019-02-17 17:44:52
【问题描述】:

所以,我有以下代码可以从我的 Firebase 用户列表中检索数据:

currentUserRef: AngularFireList<any>
currentUser: Observable<any>;

var user = firebase.auth().currentUser;

this.currentUserRef = this.af.list('usuarios', ref =>
ref.orderByChild('email').equalTo(user.email));
this.currentUser = this.currentUserRef.valueChanges();
this.currentUser.subscribe(res => console.log(res[0].condominio));

在检索到的数据中,我有两个属性:condominio 和 email。

在console.log(res[0].condominio)我有数据输出到控制台,但是我需要把它保存到一个变量中,例如:

userCond: string;

然后我必须从 res 中获取属性 'condominio' 并将其设置为 'userCond',但如果我尝试这样做它不起作用:

this.currentUser.subscribe((res) => { this.userCond = res[0].condominio });

我做错了什么?

"angularfire2": "^5.0.0-rc.11",
"firebase": "^5.3.1",
"ionic-angular": "3.9.2",

编辑:

    export class HomePage {

    currentUserRef: AngularFireList<any>
    currentUser: Observable<any>;

    moradorRef: AngularFireList<any>
    morador: Observable<any>;

   userCond: string;

   constructor(
    public authService: AuthService,
    public userService: UserService,
    public navCtrl: NavController,
    public navParams: NavParams,
    public af: AngularFireDatabase
   ) {

   }

   ionViewCanEnter(): Promise<boolean> {
     return this.authService.authenticated;
   }

   ionViewDidLoad(){
    debugger;
    //TRAZ AS MESMAS INFORMAÇÕES
    //this.userService.getCurrentUser();
    var user = firebase.auth().currentUser;

    //const _this = this;

    this.currentUserRef = this.af.list('usuarios', ref =>
    ref.orderByChild('email').equalTo(user.email));
    this.currentUser = this.currentUserRef.valueChanges();
    //this.currentUser.subscribe(res => 
    console.log(res[0].condominio));
    this.currentUser.subscribe(res => {
      debugger;
      this.userCond = res[0].condominio;
    });

    console.log(this.userCond);

【问题讨论】:

  • 我不知道 Firebase,但什么不起作用?您是否尝试在 HTML 中检索 userCond 的值? console.log(this.userCond) 没有在订阅下打印所需的输出。您是否尝试过在 HTML 中进行安全导航? angular.io/guide/template-syntax#safe-navigation-operator
  • 不,首先我从当前登录的用户那里得到电子邮件,然后我必须去我的用户的firebase列表中搜索该电子邮件,一旦找到从那里获取'condominio'属性那个项目。我不想在我的 HTML 中使用它,我只需要从 firebase 列表中获取这个属性。
  • 对,所以你的订阅中有这个。正如你所说,console.log() 打印正确......然后你将它分配给一个变量,然后你想要什么?您想在哪里使用该变量?在另一种方法?如果是这样,你什么时候调用其他方法?
  • 您似乎正在使用angularfire2 - 您能否分享/编辑您正在使用的angularfire2@angular/fire 版本的问题,以及更详细的代码清单?
  • 我必须将属性“condominio”的值作为字符串检索,这样我就可以使用它来循环另一个列表(名称为“condominio”)。

标签: angular typescript firebase firebase-realtime-database angularfire2


【解决方案1】:

问题是,您需要等待订阅完成才能使用类变量userCond。订阅不是同步的,只需从订阅下调用您要使用的方法。

this.currentUser.subscribe(res => {
  this.userCond = res[0].condominio;
  this.loopOverAnotherList();
});

loopOverAnotherList() {
   // you can use this.userCond here
}

【讨论】:

  • @Neelavar:它会保证如果我只在我的订阅下调用它......问题是,该方法不会使订阅内的代码变得混乱......
【解决方案2】:

userCond 定义为 Observable。

userCond: Observable<string>;

稍后,在查询 Firestore 时,将 condominio 属性值映射到 userCond

this.currentUserRef = this.af.list('usuarios', ref =>
                              ref.orderByChild('email').equalTo(user.email));
this.userCond = this.currentUserRef.valueChanges()
                                       .pipe(map(res => res[0].condominio));

现在可以订阅userCond 以在需要时获取值。

【讨论】:

    【解决方案3】:
      this.currentUser.subscribe(res => {
        this.userCond = res[0].condominio; // 'this' here refers to the observable, not the class instance
      });
    

    要解决这个问题,请执行以下操作:

      const _this = this;
      this.currentUser.subscribe(res => {
        _this.userCond = res[0].condominio;
      });
    

    【讨论】:

    • 通过这样做,我在 _this.userCond 上变得未定义。
    • userCond 应在类中声明为变量,以及 currentUsercurrentUserRef
    • 是的,我有以下内容:export class HomePage { currentUserRef: AngularFireList, currentUser: Observable 和 userCond: any; } 和 const _this = this;我已经在 ionViewDidLoad 上声明了,这就是我尝试检索值的地方。对吗?
    • hmm,我建议在你的代码中放置断点,然后尝试在浏览器控制台上打印这些值——如果这对你来说还是一种外来的调试方法,这里是一个参考developers.google.com/web/tools/chrome-devtools/javascript/…你在做什么@ 987654327@ 在ionViewDidLoad 上?不是一个好主意。 ionViewDidLoad关注的只是模板(视图),而不是来自服务器的数据
    • @jmdavalos:this 引用没有问题。ES6 的箭头操作保留调用上下文
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 2018-10-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多