1

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}

1 Answer 1

2

You didn't resolve the promise for accessing the data

this.doLogin(params).then((data=>console.log(data));

Use then operator to get the data

Update you are not using json mapping and separate the observable and promise

doLogin(params: URLSearchParams): Promise<User>{

 let promise = this.http.get(this.SERVICE_URL, new RequestOptions({"search": params}) )
        .map(response => <User>response.json())
        .catch(this.handleError);
    return promise.toPromise();
}

this.doLogin(params).then((data)=>console.log(data),()=>{});
Sign up to request clarification or add additional context in comments.

3 Comments

I just tried this, there's a slight syntax error (you have an extra bracket after .then(
When I use your example, the console says undefined where it tries to print out data
you already used then in your doLogin() method. so are you available in teamviewer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.