Plugin crashes when using MongoDB session adapter
dannylin108 opened this issue · 3 comments
dannylin108 commented
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
Can you reproduce this with the latest version of the plugin and empty session data?
dmk-web commented
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
This will be fixed by #77.