whoamiW
Convex Community3y ago
13 replies
whoami

Too many open queries

Unhandled Runtime Error
Error: [CONVEX Q(chats.js:get)] Uncaught Error: Too many open queries (64 > maximum 64)
  at performSyscall (../../node_modules/convex/src/server/impl/syscall.ts:21:20)
  at startQuery [as startQuery] (../../node_modules/convex/src/server/impl/query_impl.ts:188:25)
  at [Symbol.asyncIterator] (../../node_modules/convex/src/server/impl/query_impl.ts:235:4)
  at collect [as collect] (../../node_modules/convex/src/server/impl/query_impl.ts:297:19)
  at <anonymous> (../convex/chats.ts:63:15)
  at map [as map] 


Where did I do wrong? Here is my mutation that causes the error (I think) My code might be messy so any advices not related to the issue but related to my convex code in general is appreciated as well

export const get = query({
  args: {
    id: v.id('chats'),
  },

  handler: async ({ db, auth }, { id }) => {
    const identity = await auth.getUserIdentity()
    if (!identity) {
      throw new Error('Unauthenticated call to query')
    }

    const chat = await db.get(id)
    if (!chat) {
      return {
        error: 'Chat not found',
      }
    }

    const tools = await Promise.all(chat.tools.map((t) => db.get(t)))
    const messages = await db
      .query('messages')
      .withIndex('by_chat', (q) => q.eq('chat', id))
      .order('asc')
      .collect()

    const messagesWithSteps = await Promise.all(
      messages.map(async (m) => {
        const steps = await db
          .query('steps')
          .withIndex('by_message', (q) => q.eq('message', m._id))
          .order('asc')
          .collect()

        return {
          ...m,
          steps,
        }
      }),
    )

    return {
      chat: {
        id: chat._id,
        title: chat.title,
        // get the last 100 messages
        messages: messagesWithSteps.splice(
          messages.length - 100,
          messages.length,
        ),
        tools,
        status: chat.status,
      },
    }
  },
})
Was this page helpful?