The returned JSON has a structure like this:
{
"endocrinologist": {
"en_name": "Endocrinologist",
},
"dermatologist": {
"en_name": "Dermatologist",
},
"dentis": {
"en_name": "Dentist",
},
"neurosurgeon-brain-spine": {
"en_name": "Neurosurgeon (Brain & Spine)",
},
}
I have a services .js
file where I'mfetching the data and returning it like:
function getList() {
let fullURL = MY_FULL_URL
return new Promise((res, rej) => {
try {
axios.get(fullURL).then((response) => {
res(response.data)
})
} catch (error) {
rej(error)
alert(error)
}
})
}
The in the class where I want to render the component based on the fetched data goes like this:
const [meds, setMeds] = useState([])
async function getMedList() {
const resp = await bookingServices.getList()
setMeds(resp)
}
useEffect(() => {
getMedList()
}, [])
...
{meds.map((s, i) =>
<MedsCard title={s.en_name} />
)}
When I run the app I get
TypeError: undefined is not a function (near '...meds.map...')
I tried one solution which was to change setMeds(resp)
to setMeds(JSON.parse(resp))
as the JSON I'm getting is a single object, inside o which I've other single objects. This stopped the error but I get this
Possible Unhandled Promise Rejection (id: 10): SyntaxError: JSON Parse error: Unexpected identifier "object"
So, how do I correctly parse this JSON?