grammyjs / conversations

Conversational interfaces for grammY.
https://grammy.dev/plugins/conversations
MIT License
53 stars 17 forks source link

Plugin crashes when using MongoDB session adapter #63

Closed dannylin108 closed 1 year ago

dannylin108 commented 1 year ago

I'm using MongoDB session adapter like this:

interface SessionData {
    counter: number
}

type MyContext = Context &  ConversationFlavor & SessionFlavor<SessionData>

const sessions = mongoose.connection.db.collection<ISession>("sessions");
// ....

bot.use(session({
        initial: () => ({ counter: 1}),
        storage: new MongoDBAdapter<SessionData>({ collection: sessions }),
    }));

 bot.use(conversations<MyContext>());
 bot.use(createConversation<MyContext>(setStartConversation, 'setStart'));
 bot.use(createConversation<MyContext>(noStartConversation));

So the bot crashes on a first update with the message:

Error in middleware while handling update 611149669 TypeError: Cannot read properties of null (reading 'forEach')
    at ConversationHandle._replayWait (/home/danny/local/qmbotjs/node_modules/@grammyjs/conversations/out/conversation.js:451:15)
    at run (/home/danny/local/qmbotjs/node_modules/@grammyjs/conversations/out/conversation.js:204:45)
    at runUntilComplete (/home/danny/local/qmbotjs/node_modules/@grammyjs/conversations/out/conversation.js:312:32)
    at /home/danny/local/qmbotjs/node_modules/@grammyjs/conversations/out/conversation.js:359:32
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /home/danny/local/qmbotjs/node_modules/grammy/out/composer.js:61:13
    at async /home/danny/local/qmbotjs/node_modules/@grammyjs/conversations/out/conversation.js:267:9
    at async /home/danny/local/qmbotjs/node_modules/grammy/out/composer.js:61:13
    at async /home/danny/local/qmbotjs/node_modules/grammy/out/convenience/session.js:65:9
    at async /home/danny/local/qmbotjs/node_modules/grammy/out/composer.js:61:13

If using default memory session adapter, everything is smooth.

KnorpelSenf commented 1 year ago

Can you reproduce this with the latest version of the plugin and empty session data?

dmk-dark commented 1 year ago

Hello. I faced the same issue. I'm using the latest pkg version. The problem appears when user exit the session, then a null value is recorded in the conversation field in MongoDB. I think adding null & undefined checks here will be enough to prevent the problem from recurring.

KnorpelSenf commented 1 year ago

This will be fixed by #77.