-
Hello guys, import { useQuery } from "@tanstack/react-query"
import axios from "@/config/axios"
import { Chat, IGiftedChat } from "../utils/types"
const useGetChat = (id: string) => {
const query = useQuery({
queryKey: ["chat", id],
queryFn: async () => {
const response = await axios.get<Chat>(`/chats/${id}`)
return response.data
},
select: (data): IGiftedChat => {
return {
id: data.id,
user: data.contact,
messages: data.messages.map((message) => ({
_id: data.id,
text: message.content,
createdAt: new Date(message.updatedAt),
user: {
_id: data.contact.id,
name: data.contact.firstname,
avatar: data.contact.avatar ?? undefined,
},
})),
}
},
})
return query
}
export default useGetChat when I try to use it like that: Can someone help me? |
Beta Was this translation helpful? Give feedback.
Answered by
ekimkael
Jul 18, 2024
Replies: 1 comment 3 replies
-
Check that the useQuery({
// ...
select: (data): IGiftedChat => ({
messages: data?.messages?.map((message) => ({
// ...
})) ?? [],
}),
}) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My apologies guys. the problem was coming from the API.
In the route to get a chat I though I was returning an object but I was returning an array of objects