TypeScript Angular 2 Subscription Responses

I am trying to return information from my api, but I do not understand how to use the subscription correctly. With an array, I return an empty array from my service and paste the values ​​into it when I receive them.

How to correctly return only one value variable in app-component ts. Right now, if I do an alert(JSON.stringify(authenticated)) , it just gives me {"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}

app-component ts

 checkAuthentication(){ var authenticated = this._authService.getAuthenication(); } 
Authentication service

  getAuthenication() { return this._http.get('auth.php').subscribe(res => { if (res.json().authenticated === true){ return true; } return false; }); } 

auth.php

 <? session_start(); $authenticated = ( isset($_SESSION["authenticated"]) ? true : false ); $post_data = json_encode(array(authenticated => $authenticated)); echo $post_data; ?> 
+5
source share
1 answer

change

 return this._http.get('auth.php').subscribe(res => { 

to

 return this._http.get('auth.php').map(res => { 

and name it like

 this._authService.getAuthenication() .subscribe(value => this.authenticated = value); 
+7
source

Source: https://habr.com/ru/post/1243461/


All Articles