Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm building a telegram bot but I'm getting late data

I have an interface in a Telegram bot, I make it through Node.js and buttons, when you click on which you need to enter data.
I use mongoDB and store all user data in it. I have a PlusMinus value in it, which should help me find out what the user wants to do, take away from the balance or add.
There is also a button to change plus to minus, but when you click on this button, the old value is returned to me. That is, if there was a plus, I wanted to change it to a minus, I press the button – I get a plus again, and when I use it again, there will be a change to a minus. How can I fix this delay?

User.updateOne({telegramID: query.from.id}, {$set: {AssetPlusMinus: 'Minus'}})
                        .then(user1 => {
                            user1.save
                            bot.deleteMessage(chatID, msgID)
                        })
bot.sendPhoto(chatID, PhotoCrypto, {
        caption: textCrypto,
        reply_markup: {
            inline_keyboard: keyboard.AssetMenu(PlusOrMinus)
        }
    })

I tried many methods in which the function will be launched only after the execution of another function, but it did not help me

Clarification:
When you click on the change button, the old value is returned to me, but a new one is put in the database, that is, it changes but does not have time to be processed (As I understand it)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m new to this

>Solution :

Try out the updated code which addresses the delay issue:

User.updateOne({ telegramID: query.from.id }, { $set: { AssetPlusMinus: 'Minus' } })
  .then(() => {
    return User.findOne({ telegramID: query.from.id }); // Fetch the updated user data
  })
  .then(user => {
    // Use the updated user data
    user.save();
    bot.deleteMessage(chatID, msgID);
    bot.sendPhoto(chatID, PhotoCrypto, {
      caption: textCrypto,
      reply_markup: {
        inline_keyboard: keyboard.AssetMenu(user.AssetPlusMinus)
      }
    });
  })
  .catch(error => {
    // Handle any errors that occurred during the update or fetch operations
    console.error(error);
  });

🥂 Cheers!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading