I'm dealing with a particular situation involving some code:
...
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',
}
}),
...
Here's where things start to get complicated:
const {
data,
isLoading,
error
} = useGetUserInfoQuery()
When the error
occurs, it contains the following information:
{"status":500,"data":{"status":"FAILED","message":"Some message I want to display."}}
The issue arises when trying to access data.message
in error
:
error.data.message
This results in a TypeScript compiler error:
Property data does not exist on type FetchBaseQueryError | SerializedError Property data does not exist on type SerializedError
Even attempts like error!!.data!!.message!!
do not work. So how can I successfully access data.message
in error
?