I am making an api call from React app with axios.
The user selects 2 values from 2 dropdowns, and the api call is made using these 2 values. Normally the response will be an object, but occassionally if the user selects 2 values which dont have a match, then it wont return anything.
I want to ensure a message is displayed if nothing exists for those 2 items.
Currently in my axios response if nothing is return I get a long bunch of what looks like the index html page.
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.adebe8fe.js"></script><link href="/static/css/main.aa245bf1.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
I am getting the data from a c# controller,
[HttpGet("search/{antifungal}/{interacting}")]
public async Task<ActionResult<DrugInteractionDTO>> getAntifungalInteraction(string antifungal, string interacting)
{
var interactions = await _context.DrugInteractions
.Include(di => di.AntifungalAgent)
.Include(di => di.InteractingDrug)
.Where(di => di.AntifungalAgent.Name.ToLower() == antifungal.ToLower())
.Where(di => di.InteractingDrug.GenericName.ToLower() == interacting.ToLower())
.Select(x => new DrugInteractionDTO
{
Severity = x.Severity,
SeverityAsString = x.Severity.ToString(),
ProDetailedInformation = x.ProDetailedInformation,
BasicDetailedInformation = x.BasicDetailedInformation,
Antifungal = x.AntifungalAgent.Name,
InteractingDrug = x.InteractingDrug.GenericName,
ID = x.ID
}).FirstOrDefaultAsync();
if (interactions == null) return NotFound();
return interactions;
}
Using swagger if I enter 2 items in the query that dont exist I get
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
"title": "Not Found",
"status": 404,
"traceId": "00-29b4bb56b99fe26416be4e3c09add6b7-dc13efbf731e6a72-00"
}
So I would have thought calling console.log(response.data) in my axios call would log 404, but it just gives
undefined`
This is currently what I have in React,
const getInteractionDetails = () => {
agent.drugInteractions.getIndividual(antifungalDrug, interactingDrug)
.then((response) => {
console.log(response.status)
if(response.status === 404){
setError('No interaction exists')
} else {
setInteractionDetails(response)
}
})
.catch(error => console.log(error))
}
How come status isnt being shown in my axios response?