I am working on a Blazor WASM web app that gets ETAs for customer orders, but my GetFromJsonAsync() has the correct number of objects (customers) but they are all empty objects.
The data comes from an excel spreadsheet and then is mapped into objects (Customer, Order, Item) with the relations being customers have a list of orders that have a list of items. Each customer is added to a static list of Customers in the Customer class. Everything is working up to this point (I created a program just to test this and all the data is going where it belongs).
From there I create a repository of customers, and run through the static list of customers, pushing each customer to the repository.
public static void AddCustomerRepository(this IServiceCollection services)
{
Workbook workbook = new Workbook();
workbook.InitWorkbook();
workbook.MapOrdersFromWorkbook();
var customerRepository = new MemoryRepository<Customer>();
foreach (Customer customer in Customer.Customers) {
customerRepository.Add(customer);
}
services.AddSingleton<IRepository<Customer>>(customerRepository);
}
AddCustomerRepository is ran on startup of the web app. This is the JsonAsync in the .razor file, it is getting the correct number of objects in the json (814) but there is no data so it's 814 empty objects.
private Customer[] customer;
protected async override Task OnInitializedAsync()
{
customer = await Http.GetFromJsonAsync<Customer[]>("api/OrderStatus");
StateHasChanged();
}
Here is the API controller:
[Route("api/[controller]")]
[ApiController]
public class OrderStatusController : ControllerBase
{
private readonly IRepository<Customer> _customerRepository;
public OrderStatusController(IRepository<Customer> customerRepository)
{
_customerRepository = customerRepository;
}
[HttpGet]
public IEnumerable<Customer> Get()
{
return _customerRepository.GetAll()
.OrderBy(customer => customer.number);
}
}
What would cause it to get the correct number of objects but no data in the objects? This is my first time working with Blazor WASM, as well as my first time working with .NET and I definitely bit off a bigger project than I should have to start, but I'm now so far in the project that I don't want to scrap it.
CodePudding user response:
What would cause it to get the correct number of objects but no data in the objects?
Most likely a mismatch in the property names.
There are 2 classes for Customer,
Compare them. Apparently they are not "Json compatible".
The more practical approach is to have 1 class Customer in the Shared Project. See if you can make that work.
... to a static list of Customers in the Customer class.
Move that to some other place.
If you do need two different Customer classes (that is possible) then change your API. Convert Server.Customer to Shared.Customer and return that. You can use AutoMapper.