I have a login component where the user inputs it's e-mail and password. The component sends the fields to the service, where i make a http post request to my nodejs backend to know if the user exists and if password is correct.
The problem is that the response from the server is undefined, but I can get the value of the response from the network of chrome.
I believe the problem is that the request is asynchronous, and so it returns the response before it actually gets the response.
After searching I couldn't find very useful information on how to make the request synchronous.
Here are the codes of my component and of service!
In my component:
login_function(){
this.token = this.global.check_login(this.login_user,this.login_pass)
.subscribe(data => this.postData = JSON.stringify(data));
console.log(this.token);}
}
In the service:
check_login(user, pass) {
let data = {user:user,password:pass};
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, method:'post' });
console.log("user:"+user+"\npass:"+pass);
return this.http.post(url, {username:user,password:pass}, options)
.map((response:Response)=> response.json());
}