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 res
has a value of [{"QuestId":1700}]
, then it's an array; if you're sure you want to get the first entry, you have to do res[0].QuestId
instead of res.QuestId
. The error is somewhat indicative - an array doesn't have a property QuestId
.