1

I'm moving from AngularJS to Angular 6, in JS I used $resource and when I got data back that looked like this...

{
  "SomeList": [{
      "SomeID": "123456",
      "SomeName": "Joe Shmoe",
      "SomeCode": "987654",
      "Address1": null,
      "Address2": null,
      "City": null,
      "State": null,
      "Zip": null,
      "Phone1": null,
      "Phone2": null,
      "Email": null

    },
    {
      "SomeID": "234567",
      "SomeName": "Joe Bagodonuts",
      "SomeCode": "456123",
      "Address1": null,
      "Address2": null,
      "City": null,
      "State": null,
      "Zip": null,
      "Phone1": null,
      "Phone2": null,
      "Email": null
    },
    etc...
  ]
}

It just flowed into a variable very nicely and I could use the data.

In Typescript I have set up...

A Model

export class SomeList {
  SomeID: string;
  SomeName: string;
  SomeCode: string;
  Address1: string;
  Address2: string;
  City: string;
  State: string;
  Zip: string;
  Phone1: string;
  Phone2: string;
  Email: string;
}

export class SimpleResponse < T > {
  Data: T;
}

export class SimpleResponseLower < T > {
  data: T;
}

A variable set to the model in a singleton

public static somies: SomeList[];

A Data Service

getSomeList<SomeList>(year: number): Observable<SomeList[]>{
  const url = `${Urls.Somies()}?cropYear=` + year;
  var host = window.location;
  var combineurl = host.href + 'api/RequestHandler?uri=' + url;
  return this.http.get<SimpleResponse<SomeList[]>>(combineurl).pipe(map(r => 
r.Data));

 ***The call below is what returned the sample data above
 //return this.http.get(combineurl).pipe(map(value =>{return value}));

  }
}

and A call to the data service filling the class

this.dataService.getSomeList < SomeList > (2018)
  .subscribe((data) => {
      this._formValues.somies = data;
    },
    () => {
      // put some code here and remove console.log
    }); // end of error block);
}

I've tried just about every configuration I can think of and data is coming back "undefined" with no errors and the link listed in the Network tab of the browser populating.

Any help or ideas are greatly appreciated!!

17
  • are u getting the data by browsing the url directly in the browser? Commented Sep 6, 2018 at 17:02
  • I might be wrong, but the data you're getting back doesn't have a key Data, and that's what you seem to be mapping to. Maybe Data is undefined? Have you tried returning an array of SomeList, since that's what the server is returning? Commented Sep 6, 2018 at 17:03
  • I am getting data back by browsing. Data is part of SimpleReponse Commented Sep 6, 2018 at 17:07
  • But the data you said you got back doesn't say Data anywhere, and your model should correspond to what you get back. I believe what you get back would have to look something like this: { "Data" : { "SomeList": [ { "SomeID": "123456", "SomeName": "Joe Shmoe", "SomeCode": "987654", "Address1": null, "Address2": null, "City": null, "State": null, "Zip": null, "Phone1": null, "Phone2": null, "Email": null } etc...]} }, if you wanted to map your response to the Data property. There doesn't seem to be an indication of a SomeList property either. Commented Sep 6, 2018 at 17:09
  • can you console.log(r) return this.http.get<SimpleResponse<SomeList[]>>(combineurl).pipe(map(r => console.log(r))); and share what does this print. @Funn_Bobby Commented Sep 6, 2018 at 17:20

2 Answers 2

1

Your JSON should map directly to your models.

Either your data should be:

'{
   "Data": [
      {
        "SomeID": "123456",
        "SomeName": "Joe Shmoe",
        "SomeCode": "987654",
        "Address1": null,
        "Address2": null,
        "City": null,
        "State": null,
        "Zip": null,
        "Phone1": null,
        "Phone2": null,
        "Email": null
      }, etc...
   ]
}

Or your simple response should expect SomeList:

export class SimpleResponse<T> {
   SomeList: T;
}

And you should map to the SomeList property on the response instead of Data.

return this.http.get<SimpleResponse<SomeList[]>>(combineurl).pipe(map(r 
 => r.SomeList));
Sign up to request clarification or add additional context in comments.

11 Comments

Tried changing SimpleResponse and mapping to SomeList...still doesn't work...could it be that the object coming back is a JObject? ...and needs to be something else?
It should be a plain javascript object. Have you tried not typing it at all, and logging what you get back? It might help you figure out what the structure of the object you get back is.
this.http.get<any>(combineurl).pipe(tap(data => console.log(data)); Something like that to see what you get.
Yes...It looks exactly like what I posted in the original question.
The error I'm getting while trying to write it out now is ERROR Error: Cannot find a differ supporting object '{ "AgencyList": [ My select control looks like this
|
0

Okay, I was able to figure this out. What I had to do was...

Change the type being returned from api from

string jsonString = client.DownloadString(fullUri);                                
JObject infoObj = JsonConvert.DeserializeObject<dynamic>(jsonString);
string data = infoObj.ToString();                                   
return data;

To

string jsonString = client.DownloadString(fullUri);                                
JObject infoObj = JsonConvert.DeserializeObject<dynamic>(jsonString);
SimpleResponse<JObject> data = new 
SimpleResponse<JObject>();
data.Data = infoObj;                                
return data;

and on the typeScript side I had to change the model to

export class SimpleResponse<data> {
data: data;
}
export class SomeResponse{
SomeList: SomeList[];
Message: string;
}

and in my data service instead of returning...

<SimpleResponse<SomeList[]>>

I return

<SimpleResponse<SomeResponse>>

I'm not sure if this is the "TypeScript" way of doing it or if there is a better more generic way so I don't have to add a ResposeClass for each call I make to the API...but it is working!!

1 Comment

I'm glad things are working out for you now! That was a long and difficult ordeal :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.