I have this slice
...
baseQuery: fetchBaseQuery({
baseUrl: BASE_ENDPOINT,
prepareHeaders: headers => {
const token = Cookies.get(TOKEN_COOKIE)
if (token) headers.set('authorization', `Bearer ${token}`);
return headers;
},
}),
getUserData: builder.query<SomeProps, void>({
query: () => ({
url: "...",
headers: {
'Content-Type': 'application/json',
}
}),
...
At one point in this
const {
data,
isLoading,
error
} = useGetUserInfoQuery()
error becomes true and contains this
{"status":500,"data":{"status":"FAILED","message":"Some message I want to display."}}
However, the TypeScript compiler complains about this
error.data.message
It says
Property data does not exist on type FetchBaseQueryError | SerializedError Property data does not exist on type SerializedError
I cannot even do this
error!!.data!!.message!!
How would I access data.message in error?
The
errortype isunknown, so you'll need to cast it to an appropriate type if you would like to access specific properties.See useQuery hook declarations:
Example usage: