I have a useQuery hook which calls an api and returns the response like :
return {
title : response?.data?.[0]?.title,
desc: response?.data?.[0]?.desc,
}
I have the useQuery like this :
const {data: content} = useQuery('entitle', apiCall)
Here, I am trying to destructor this data like :
const {data: {title : [], desc: []}} = useQuery('entitle', apiCall)
Now, it gives can not read property title of undefined while using it . How can I fix this ?
CodePudding user response:
First do
const {
data
} = useQuery('entitle', apiCall)
console.log(data)
And pls check if data
has the structure you expect. If the request is done alright, then I guess you must expect the log to look like this.
{
title: "something",
desc: "something-else"
}
If the structure of the object is not like that or you get undefined
. Then you got a problem either with the request or the hook call, somehow the query is not returning what you desire.
If the structure is the one you expect it to be, then write what good Phil
wrote earlier in your post's comment.
const {
data: {
title = [],
desc = []
}
} = useQuery('entitle', apiCall)
CodePudding user response:
Dont complicate the things
const {data} = useQuery('entitle', apiCall)
const {title=[], desc=[]} = data