T
TanStack3w ago
other-emerald

Hitting issues updating an item in collection

export const createConversationThoughtsCollection = (
conversationId: string
) => {
return createCollection(
queryCollectionOptions({
queryKey: ["conversation-thoughts", conversationId],
id: `conversation-thoughts-${conversationId}`,
schema: ThoughtSchema,
getKey: (thought) => thought.id,
queryFn: async () => {
const response = await listConversationThoughts({
query: { conversation_id: conversationId },
});

// Transform API response to match schema
return (
response?.data?.thoughts?.map((thought) => ({
...thought,
topic_id: thought.conversation_id, // Map conversation_id to topic_id
archived_at: thought.archived_at as Date | null | undefined,
reply_to_id: thought.reply_to_id as string | null | undefined,
})) ?? []
);
},
queryClient,
onInsert: async ({ transaction }) => {
...
},

onUpdate: async ({ transaction }) => {
...
},
onDelete: async ({ transaction }) => {
...
},
})
);
};
export const createConversationThoughtsCollection = (
conversationId: string
) => {
return createCollection(
queryCollectionOptions({
queryKey: ["conversation-thoughts", conversationId],
id: `conversation-thoughts-${conversationId}`,
schema: ThoughtSchema,
getKey: (thought) => thought.id,
queryFn: async () => {
const response = await listConversationThoughts({
query: { conversation_id: conversationId },
});

// Transform API response to match schema
return (
response?.data?.thoughts?.map((thought) => ({
...thought,
topic_id: thought.conversation_id, // Map conversation_id to topic_id
archived_at: thought.archived_at as Date | null | undefined,
reply_to_id: thought.reply_to_id as string | null | undefined,
})) ?? []
);
},
queryClient,
onInsert: async ({ transaction }) => {
...
},

onUpdate: async ({ transaction }) => {
...
},
onDelete: async ({ transaction }) => {
...
},
})
);
};
When I call
thoughtsCollection.update(state.editingThoughtId, (draft) => {
draft.message = state.message.trim();
draft.voice_id = state.selectedVoiceId!;
});
thoughtsCollection.update(state.editingThoughtId, (draft) => {
draft.message = state.message.trim();
draft.voice_id = state.selectedVoiceId!;
});
I get
Error updating thought: [CollectionOperationError: The key "6829b561-d0bc-409e-a656-4ff0bd46ed61" was passed to update but an object for this key was not found in the collection]
Error updating thought: [CollectionOperationError: The key "6829b561-d0bc-409e-a656-4ff0bd46ed61" was passed to update but an object for this key was not found in the collection]
I check and there are no keys within the collection, but it's somehow showing the data I'm rendering.
console.log("keys:", thoughtsCollection.keys()); => {}
console.log("keys:", thoughtsCollection.keys()); => {}
2 Replies
other-emerald
other-emeraldOP3w ago
const { data: thoughtsData, isLoading: thoughtsLoading } =
useLiveConversationThoughtsApiQuery(conversationId);
console.log(
"Thoughts data:",
thoughtsData.map((t) => t.id)
); => list of keys
const { data: thoughtsData, isLoading: thoughtsLoading } =
useLiveConversationThoughtsApiQuery(conversationId);
console.log(
"Thoughts data:",
thoughtsData.map((t) => t.id)
); => list of keys
wise-white
wise-white3w ago
only thing I can think of is you're perhaps updating the wrong collection?

Did you find this page helpful?