Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

property then does not exist on type void , A typescript error

Code:

 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');
    }
  })
}

Error:

property then does not exist on type void , A typescript error

can someone please help me by correcting this code snippet so that the 'then' function works cheers!

like image 764
Kanav Malik Avatar asked Aug 01 '17 04:08

Kanav Malik


People also ask

Does not exist on type TypeScript?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Is not assignable to type '() => void?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

Does not exist on type string?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.


1 Answers

The issues here is with passwordreset() function ,

It should look like this :

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);
}

You were returning the promise inside promise function , but not returning it from passwordreset(),

Please have a look at your code and updated code , you will get an idea

Your code :

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

Updated Code :

passwordreset(email): Promise<any>
{ 
        return new Promise((resolve,reject)=>{ 
            firebase.auth().sendPasswordResetEmail(email).then(()=>{ 
                                resolve({success :true}); 
                            })
                            .catch((err)=>{ 
                                reject(err); 
                            }); 
        }); 
}
like image 67
Vivek Doshi Avatar answered Oct 08 '22 11:10

Vivek Doshi