【问题标题】:Null object in promise Angular2承诺Angular2中的空对象
【发布时间】:2016-12-10 04:39:21
【问题描述】:

我正在开发一个 Ionic2 应用程序并且我正在使用 Promise。在 Typescript 类中,我创建了这个方法

tapOnRegistrati() {
this.authService.userRegistration(this.email,this.password).then(function(user){

  this.user = this.af.database.object('/users/' + user.uid);
  this.user.set({ name: this.name, lastname: this.lastname});


}).catch(function(error: any) {

});

}

但是当then 块被执行时this 的引用是null,所以我不能使用我的打字稿类的任何对象/属性。为什么?

【问题讨论】:

    标签: angular firebase firebase-realtime-database ionic2


    【解决方案1】:

    因为如果你使用你的回调函数,比如

    .then(function(user){
      this.user = this.af.database.object('/users/' + user.uid);
      this.user.set({ name: this.name, lastname: this.lastname});    
    })
    

    this 将引用回调中的函数对象。如果要引用该页面,请使用:

    then((user)=>{
      this.user = this.af.database.object('/users/' + user.uid);
      this.user.set({ name: this.name, lastname: this.lastname});
    
    })
    

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      相关资源
      最近更新 更多