The fetch statement getting json data from the server. I want to get the value of the object to be saved in state. The response is only one element in it [{"QuestId":1700}]. How to get the value of QuestId. My code is giving error of undefined.
fetch("http://testapi/quest", {
.then(res => res.json())
.then(
res => {
setQuestId(JSON.stringify(res.QuestId))
console.log('Test' + qid);
},
(error) => {
alert("Failed" + error);
}
);
If
reshas a value of[{"QuestId":1700}], then it's an array; if you're sure you want to get the first entry, you have to dores[0].QuestIdinstead ofres.QuestId. The error is somewhat indicative - an array doesn't have a propertyQuestId.