I created a service in angular named UserService, and a function in app.componenet.ts that invites it. Here is the code in the UserServer:
//login function in the UserService – gets user's id and returns his name
Login(userId: string):Observable<string>
{
let headers=new Headers({'Content-type':'application/json;charset=utf-8'});
return this.http.get<string>(`${this.url}/login/${userId}`);
}
and this is the function in the app.componenet.ts:
Login()
{
this.userService.Login(this.userID).subscribe(n=>{
alert('your name is' n)
});
}
The problem is that the Login function in the componenet doesn't enter the subscribe function. It enters the Login function in the Service but not to the subscribe. What should I do?
I followed the code with debugger and didnt see anything strange
CodePudding user response:
Login(userId):Observable<any>{
return this.http.get(`${this.url}/login/${userId}`);
}
on your app component
Login(){
this.userService.Login(this.userID).subscribe(n=>{
alert('your name is' n)
});
}
if you have no other problems in your code it should work
in case it doesn't work you can do a try catch and show us the error
Login(){
try{
this.userService.Login(this.userID).subscribe(n=>{
alert('your name is' n)
});
}catch(error) console.log(error)
}
CodePudding user response:
I don't see an issue with the code in itself. Here's a simple implementation using a subscribe with a get method.
Make sure your get works properly