Issue
I have this in my repository:
public async Task<IEnumerable<CatalogModel>> GetCatalogByName(string _UserId, string _CatalogName)
{
var data = await dbcontext.Catalog.Where(x => x.UserId == _UserId).ToListAsync();
return mapper.Map<IEnumerable<CatalogModel>>(data);
}
And currently, this in my Controller:
[HttpGet]
public IActionResult GetCatalogsByName([FromQuery] string UserId, string CatalogName)
{
var task = repository.Catalog.GetCatalogByName(UserId, CatalogName);
return Ok(task);
}
So right now I am returning Ok(task) all the time. I would like to check if there is data returned from the repository or not so I can also return NotFound(task). I could not seem to figure out how to do that.
Solution
You will need to wait for the GetCatalogByName
to complete before examining the result.
A simple await
will do
[HttpGet]
public IActionResult GetCatalogsByName([FromQuery] string UserId, string CatalogName)
{
var task = await repository.Catalog.GetCatalogByName(UserId, CatalogName);
// check task data before return
return Ok(task);
}
But I strongly recommend you to read more about async/await programming here
Answered By – Tuan Tran
Answer Checked By – David Goodson (BugsFixing Volunteer)