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)
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!