1

I am trying to get all equipment types from my API using the following code.

client = new JsonServiceClient(environment.apiEndpoint);
var equipmentTypes = new GetEquipmentTypes();
var response = this.client.get(equipmentTypes);

I can see that it is in the network tab. The data is being transferred.

public class GetEquipmentTypeResponse

{
    public IEnumerable<EquipmentType> Results { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}

Is the return DTO from the API.

[Route("/api/EquipmentTypes", "GET")]
public class GetEquipmentTypes : IReturn<GetEquipmentTypeResponse>
{

}

Is the ServiceInterface used.

IEnumerable<EquipmentType> response = db.Select<EquipmentType>(x=>x.Name == request.Name);
return new GetEquipmentTypeResponse { Results = response, 
                                     ResponseStatus = new ResponseStatus { }};

Is what the API returns.

The API is written in asp.net. The client side is angular 6 (typescript).

I have attached two images, which is the request and the response given.

Request This is the request which is sent to the API.

Response from API This is what the API responds.

What I get in ts. This is what I get from var response. (console.log(response))

Atle Kristiansen
  • 707
  • 7
  • 28
  • It's impossible for anyone to diagnose any potential issues without any info on what's actually being sent or what the response is. It's not even clear what language is being used as the code suggests C# but `client.get()` is not an API in C# clients. At a minimum you should show the full HTTP Request/Response Headers, the API endpoints/BaseUrl used. Also not cause of the issue but you [shouldn't use Interfaces in DTOs](https://stackoverflow.com/a/10759250/85785) (just use List) or populate `ResponseStatus` unless you want to return a custom Error Response. – mythz Mar 28 '19 at 23:30
  • Yeah, sorry for the lack of details. I have updated the question now. Do you mean that I should not return IEnumerable of equipmentType? just a List. I tried to follow the example from https://docs.servicestack.net/api-design. – Atle Kristiansen Mar 29 '19 at 17:07
  • 1
    Right don't use interfaces in DTOs, use Concrete collections like `List` instead. – mythz Mar 29 '19 at 17:09

1 Answers1

1

The screenshot shows that the response is being returned fine, the Promise result is just not being awaited, try:

var response = await this.client.get(equipmentTypes);
mythz
  • 141,670
  • 29
  • 246
  • 390
  • As always your answer is spot on. When I get a promise, this would usually mean that this should be awaited? – Atle Kristiansen Mar 29 '19 at 17:40
  • 1
    @AtleKristiansen A Promise is like a Task in C#, you need to await or attach a continuation to run when the future value is resolved. See [Understanding promises in JavaScript](https://hackernoon.com/understanding-promises-in-javascript-13d99df067c1) – mythz Mar 29 '19 at 17:43