Looking for clarification with Angular observable code

I’m working through some of the tutorials found Angular website and I’m somewhat stuck on a particular example and need some clarification. The example on the website is using hard coded data (mock database), and I replaced the mock database with actually database (PHP) and the connection to the database and quering the db works…

issue is round the two lines of code you see below(hightlighted):

Two particular lines in question are:
return body.data || { }; // As per angular website If I use this line of code I get a undefined results.
return body; // If I use this line of code; removing “data || {}” the application works.

I’m assuming it has something to do with the format the data is received…

Read another post and it stated that I needed a wrapper. Not sure what wrapper which confused me even more. so how do I go about getting this line of code return body.data || { }; to work with my application and more specifically “|| { }” doing ?

any help you can provide would be greatly appreciated

service.ts

getUsersFromDatabase(): Observable<PersonType[]>{
        return this.http
            .get(this.userUrl)
            .map(this.extractData)           
            .catch(this.handleError);            
        }   
         private extractData(res: Response) {
               let body = res.json();
             //return body.data || { };  // line One. error received as: undefined
              return body;     // Line Two -  works
    }

component.ts

userListObs: PersonType[];
    constructor(private obsListservice: GetListObsService){}
    ngOnInit(): void{
        this.getObservableMethod();
      }
    getObservableMethod() {
        this.obsListservice.getUsersFromDatabase()
            .subscribe(
             uList => this.userListObs = uList,
             error =>  this.errorMessage = <any>error); 
    }

php script for database query (works)

<?php
	header("Access-Control-Allow-Origin: *");
	include('dbhandler.php');
	$dbcon = Connection::getConnection();
	try {	
                    $STMT = $dbcon ->query("SELECT * from tbl_users");
	      $userListOutput = $STMT->fetchAll(PDO::FETCH_ASSOC);					}	
              catch(PDOException $e){
                    echo 'DB error: ' . $e->getMessage(); 
	}
      echo json_encode($userListOutput); 
?>

data type

export class PersonType {
  userID: number;
  userName: string;
  password: string; 
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.