My Angular Project use a ASP.NET Core Web Api.
For one of my services when I call it in the Angular project I get an [object Object] while when I test it with Swagger I get a Json result.
In this method, when I display response in the browser console I recieved an [object Object]
getMenuByTruck = () =>{
const id: string = this.activeRoute.snapshot.params['id'];
const apiMenu: string = environment.apiAddress + 'TruckMenu/' + id;
this.repository.getData(apiMenu)
.subscribe(response => {
console.log('Menu = ' + response);
this.menu = response as Menu[];
},
(error) =>{
this.errorHandler.handleError(error);
this.errorMessage = this.errorHandler.errorMessage;
})
}
I compared my service with other ones and I haven' t found any differences.
The mapping in Angular is correct.
I don't know why I recieved a [object Object].
Any idee?
Edit : Below the method in the Web Api
[HttpGet("{truckId}", Name = "MenuByTruckId")]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<TruckMenu>> GetByTruckId(int truckId)
{
if (truckId <= 0) return BadRequest();
try
{
var truckMenus = await _repository.TruckMenu.GetByTruckIdAsync(truckId);
var truckMenusResult = _mapper.Map <IEnumerable<TruckMenuDto>> (truckMenus);
if (truckMenusResult == null) return NotFound();
return Ok(truckMenusResult);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetById action int TruckMenuController : {ex.Message} ");
return StatusCode(StatusCodes.Status500InternalServerError, "Internal Server Error");
}
}
Here is my Web Api model:
public class TruckMenuDto : DescriptiveItemDto
{
public int TruckId { get; set; }
public int FoodCategoryId { get; set; }
public virtual FoodCategoryDto FoodCategory { get; set; }
public string Title { get; set; }
public string Ingredients { get; set; }
public string Image { get; set; }
public decimal Price { get; set; }
public virtual TruckDto Truck { get; set; }
}
and the same model in angular:
export interface Menu extends DescriptiveItem{
truckId: number;
trucks: Truck;
foodCategoryId: number;
foodCategory?: FoodCategory;
title: string;
ingredients: string;
image: string;
price: number;
}