I'm currently facing an issue while using native promises with Angular and TypeScript:
export class LoginComponent implements OnInit {
public user = {};
constructor( private authService:AuthenticationService) { }
ngOnInit() {
}
login() {
console.log( 'Connecting to server' );
this.authService.login( this.user ).then(( response ) => {
// It works
console.log( response['success'] );
// There is an error
console.log( response.success );
}, ( error ) => {
console.log( error );
});
}
}
Below is a simple service that simulates a connection to the server:
export class AuthenticationService {
// ... some code ...
login( loginData ) {
let self = this;
return new Promise(function(resolve, reject){
// Simulating a delay - as there is no backend for now
setTimeout(function() {
if ( loginData.username === 'username' && loginData.password === 'password' ) {
resolve({
message: "Successfully logged in",
success: true,
errors: null
});
} else {
reject({
message: "Incorrect user data provided",
success: false,
errors: {
"username": true,
"password": true
}
});
}
}, 100);
});
}
// ... some code ...
}
I have tried searching for a solution to the
Property 'success' does not exist on type '{}'.
error without any luck.