属性然后不存在类型void,打字稿错误

时间:2017-08-01 04:27:47

标签: angular typescript typescript2.0

代码:

 reset(){
  let alert = this.AlertCtrl.create({
    buttons :['ok']
  });
  this.userservice.passwordreset(this.email).then((res: any)=>{
    if(res.success){
      alert.setTitle('Email sent');
      alert.setSubTitle('please follow the instructions in the email to reset the password')

    }
    else{
      alert.setTitle('failed');
    }
  })
}

错误:

  

属性然后在类型void,A打字稿错误

上不存在

有人可以通过更正此代码段来帮助我,以便'然后'功能有效 干杯!

2 个答案:

答案 0 :(得分:4)

这里的问题是passwordreset()函数,

它应该如下所示:

passwordreset(): Promise<any> {
  // this should return a promise
  // make sure , you are returning promise from here
  return this.http.get(url)
             .toPromise()
             .then(response => response.json().data)
             .catch(this.handleError);
}
  

你在promise函数中返回了promise,但没有   从passwordreset()

返回

请查看您的代码和更新的代码,您将获得一个想法

您的代码:

passwordreset(email)
{ 
        var promise = new Promise((resolve,reject)=>{ 
            firebase.auth().sendPasswordResetEmail(email).then(()=>{ 
                            resolve({success :true}); 
                            })
                            .catch((err)=>{ 
                                reject(err); 
                            }) 
                            return promise; 
        }); 
}

更新代码:

passwordreset(email): Promise<any>
{ 
        return new Promise((resolve,reject)=>{ 
            firebase.auth().sendPasswordResetEmail(email).then(()=>{ 
                                resolve({success :true}); 
                            })
                            .catch((err)=>{ 
                                reject(err); 
                            }); 
        }); 
}

答案 1 :(得分:0)

我使用这个

singlesender(data){

return new Promise((resolve,reject)=>{ 
  this.userService.getAccessToken().then(accesstokendata=>{
    this.accesstoken=accesstokendata;
    console.log(this.accesstoken);
    console.log("hello");

    console.log("hello2");
    let dataToSend = new HttpParams()
      .set("senderidforme", data.senderidforme)  

    let header = new HttpHeaders()
    .set("Content-type", "application/x-www-form-urlencoded")
    .set("Authorization", "Bearer "+this.accesstoken);

      return this.get(SERVER_URL+"web/v2/user-message-unreadcountsignle", dataToSend, header);
  })
  .catch(error=>{

  })


}); 


      }