Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a List of custom objects from an HTTP GET for use in an Angular 2 app

I'm trying to figure out how to properly return a list of custom objects and print its contents using an HTML file.

Here's what the HTTP GET method looks like (for simplicity's sake I've stripped some things down to only illustrate the problem) :

[HttpGet("/atr/testing")]
public List<CustomObject> GetCustomObjects()
{
    //MasterObject.CustomObjects returns a List of CustomObjects
    return MasterObject.CustomObjects();
}

My TypeScript file (called home.ts) subscribes and calls the HttpGet method - here's what the Home class looks like (I've excluded all the imports and @Component section):

export class Home {
public selectedConfiguration: string = 'TESTVALUE';

constructor(private http: Http) {
    this.getConfigurations()
        .subscribe(res => {
            this.selectedConfiguration = res;
        });
}

getConfigurations() {
    console.log('Home:getConfigurations entered...');

    return this.http.get('atr/testing')
        .map((response) => {
            console.log(response);
            return response.json();
        });
}

}

As you can see, the constructor calls 'getConfigurations' which calls the HttpGet method. The part that confuses me is how do I go about using the returned response properly? Form my understanding responses are returned as JSON strings (which is why my selectedConfiguration variable in the Home class is a string value instead of a List). When I try to print out the 'selectedConfiguration' string with {{selectedConfiguration}} in my HTML file I get this:

[object Object],[object Object],[object Object],[object Object]

If I use {{selectedConfiguration[0]}} in my HTML I only get one: [object Object]

The List returned by the GET method is supposed to have 4 CustomObject's in it and it seems like the JSON response has that at least. Each CustomObject has variables like 'Name', 'Date', 'Time', etc. I've tried printing data in my HTML using {{selectedConfiguration[0].Name}} and it prints out nothing. I'm not really sure what do to from here (or if I'm doing something wrong) in order to display each object's info.

like image 278
Roka545 Avatar asked Oct 18 '22 09:10

Roka545


1 Answers

The short answer

In your example, you're trying to convert a JSON object to a string. To get it to work, you will probably have to do:

this.selectedConfiguration = JSON.stringify(res);

The long answer

Let's say your CustomObject class looks something like:

public class CustomObject
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
}

In your Angular code, create an interface that mimics it. Verify in developer tools that the case matches ("Name" and not "name"):

export interface ICustomObject {
    Name: string;
    Date: Date;
}

Leverage the type checking of TypeScript and actually read your returned list as an array of your CustomObjects:

export class Home implements OnInit {
  public configurations: ICustomObject[];
  public selectedConfiguration: ICustomObject;

  constructor(private http: Http) {
    // move initialization code to ngOnInit. Don't forget the import and implements
  }

  ngOnInit() {
    this.getConfigurations()
        .subscribe((customList: ICustomObject[]) => {
            this.configurations = customList;
            // todo: determine the selected configuration
            this.selectedConfiguration = this.configurations[0];
        });
  }

  getConfigurations(): ICustomObject[] {
    return this.http.get('atr/testing')
        .map((response: Response) => {
            return <ICustomObject[]>response.json();
        });
}
like image 111
Brad Rem Avatar answered Nov 03 '22 19:11

Brad Rem