M
M
Michael2017-03-06 02:11:26
Node.js
Michael, 2017-03-06 02:11:26

Why such strange things with scope?

Hello. The code is like this:

switch (data) {
        case 'buy': //как купить BTC
            const keys = message.buy.key[lng];
            keys.chat_id = msg.message.chat.id;
            keys.message_id = msg.message.message_id;
            bot.editMessageText(message.buy.text[lng], keys);
        break;

        case 'sell': //как продать BTC
            const keys = message.sell.key[lng];
            bot.editMessageText(message.sell.text[lng], keys);
        break;
    }

And in the console I see something unexpected:
c7f39935a0c2404e815954820450e5a5.png
Why does the compiler swear at the keys variable in the sell block? If I do not define it and try to pass it to bot.editMessageText, then the console says: keys is not defined.
Where is the mistake?
Z. Y. The keys variable does not occur elsewhere in the program. The message and lng variables are correct and available in both blocks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-03-06
@mak_ufo

You have 2 times the declaration of const keys inside one block (the whole switch is one block)
, you can wrap each case in your own block, then it will work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question