TanStackT
TanStack4mo ago
2 replies
efficient-indigo

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 }) => {
        ...
      },
    })
  );
};


When I call
     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]


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()); => {}
Was this page helpful?