In my authorization service, I am working on verifying the existence of a user.
import { HttpClient } from "@angular/common/http";
import 'rxjs/Rx'
@Injectable()
export class AuthService {
constructor( private http : HttpClient) {}
registerUser(){
var temp = doesUserExist().subscribe(x => temp = x);
if(temp){
//register User logic
}
}
private doesUserExist(){
var endpoint = [myPath];
return this.http
.get(endpoint)
.map(payload => payload);
}
}
When I send a GET request to [myPath], the server responds with:
[{"SqlResponse":"0"}]
I am facing issues with the map and subscribe functions. The code snippet above results in logging a Subscriber
object. I tried different methods, like:
private doesUserExist(){
var endpoint = [myPath];
return this.http
.get(endpoint)
.map(payload => payload.json());
}
but encountered the error 'json() is not found on type 'Object'. I learned that .map already converts the string to JSON, so there is no need for this. I also attempted:
private doesUserExist(){
var endpoint = [myPath];
return this.http
.get(endpoint)
.map(payload => payload[0]);
}
which produced the same Subscription object in the console. I tried casting it as Observable<boolean>
private doesUserExist() : Observable<boolean>{
var endpoint = [myPath];
return this.http
.get<boolean>(endpoint)
.map(payload => payload);
}
but still ended up with a Subscriber
object.
EDIT When I use this method...
var temp; doesUserExist().subscribe(x => {temp = x; console.log(temp); });
I still receive a generic object... How can I access that in TypeScript?
https://i.sstatic.net/CvOE7.png
Can anyone provide guidance on this issue? I believed mapping the result should work, but since I am getting an array of JSON as a response, it might be causing the problem.