Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to store data into session after asynchronous method #6

Open
KrishEnacton opened this issue Feb 16, 2024 · 1 comment
Open

Comments

@KrishEnacton
Copy link

KrishEnacton commented Feb 16, 2024

// DB connection file
const store = MySQL({
  database: 'next-test',
  host: 'localhost',
  user: 'root',
  password: 'root',
  port: 8889,
});

bot.use(session({ store, defaultSession: () => ({}) }));
bot.command(commandsList.start.command, async (ctx) => {
  const { getUserInfo } = useUserApi(ctx);
  const token = ctx.update.message.text.trim().replace('/start ', '').replace('-', '|').trim();
  if (token.length > 0 && ctx.update.message.text != '/start' && token.includes('|')) {
    try {
      // this is working fine
      ctx.session.userAuthToken = token;
      const user_info: any = await getUserInfo(token);
      if (user_info) {
	// this is not working  
        ctx.session.userAuthToken = token;
        ctx.session.user_id = user_info.user.user_id;
        ctx.session.email = user_info.user.email;

        ctx.reply(`${translate('login_success_msg')}`, {
          reply_markup: {
            remove_keyboard: true,
          },
        });
      } else {
        ctx.session.userAuthToken = '';
      }
      console.log('🚀 ~ bot.command ~ ctx.session:', ctx.session);
    } catch (error) {
      console.log(error);
      ctx.reply(`${translate('something_wrong')}`, {
        reply_markup: {
          keyboard: replyWithKeyboard,
          resize_keyboard: true,
        },
      });
    }
  } else {
    if (!ctx.session.userAuthToken) {
      ctx.reply(`${translate('start_msg')}`, {
        reply_markup: {
          keyboard: replyWithKeyboard,
          resize_keyboard: true,
        },
      });
    }
  }
});
@MKRhere
Copy link
Member

MKRhere commented Feb 16, 2024

The real issue isn't here but somewhere else where you have a missing await. This usually somewhere that you have called next(), but you haven't awaited or returned it.

Also see:
https://github.com/feathers-studio/telegraf-docs/blob/master/guide/session.md#how-to-use-session-safely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants