I'm trying to create a login feature for my Angular app. I'm using Angular 4 (which it seems is nothing like Angularjs)
I'm trying to work out how to use HTTP and am just stuck.
I'm getting this error on the webpage:
login.component.ts (29,3): Type 'Promise<User>' is not assignable to type 'User'.
Property 'id' is missing in type 'Promise<User>'.
and this on the console:
login.component.ts (30,25): Property 'json' does not exist on type 'User'.
For the life of me I can't seem to get the actual response sent from the server (see below).
I've followed the HTTP tutorial on angular.io (https://angular.io/tutorial/toh-pt6) and I've tried numerous variations with .subscribe, .map, etc
This is the component that accesses the backend:
export class loginComponent {
SERVICE_URL = "http://localhost:80/play/classes/index.php?fn=userLogin";
model = new Login("", "");
user: User;
constructor(private http: Http) {}
onSubmit(){
console.log("submission complete!");
console.log(this.model.username);
console.log(this.model.pass);
let params: URLSearchParams = new URLSearchParams();
params.set('username', this.model.username);
params.set('password', this.model.pass);
this.user = this.doLogin(params);
console.log(this.user.json);
}
doLogin(params: URLSearchParams): Promise<User>{
return this.http.get(this.SERVICE_URL, new RequestOptions({"search": params}) )
.toPromise()
.then(response => response.json().data as User)
.catch(this.handleError);
}
get diagnostic() { return JSON.stringify(this.model); }
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
This is the response I get from the server but can't actually use in the app:
{"id":"3","username":"jamiemac262","password":"123456","dob":"1993-03-24","email":"[email protected]","steamID":null,"age":24}