Angular 2 - Injectable issue- unable to pass array/data

problem with injectable… I’m not sure if getdata() function is actually working.
far as the the line querying the database, it works but not sure if the variable “data” is receiving the data.

how do i determine if " return this.data" actually contains the data pulled from the database?

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {UserType} from '../usertype';

@Injectable()
export class LoginService {
    public data:UserType[];

    constructor(private http:Http){  } 
     
getData():UserType[]{
        this.http.get('http://localhost:8080/angular2/tutorial1/app/services/userlist.php')
       .subscribe(res => this.data = res.json());
       return this.data;       
    }
}

Hey @robin01,

This code won’t work, because as soon as you call getData() it will return the current value of this.data (probably undefined, unless you’re initializing it to a default value) before the HTTP request has completed.

Have a look at the Angular 2 docs for the HTTP client. There are some examples of how to deal with getting data back from HTTP requests when dealing with observables.

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