I initialize coin and then give it a value inside the if-statement:
const https = require('https');
var coin = ''
var options = {
"method": "GET",
"hostname": "rest.coinapi.io",
"path": "/v1/exchangerate/" + coin,
"headers": {'X-CoinAPI-Key': 'secretkey'}
};
client.on('message', msg => {
if (msg.content === 'money') {
msg.reply('nice');
}
if (msg.content === 'BTC/USD') {
coin = msg.content;
var request = https.request(options, function (response) {
response.on('data', d => {
var json = JSON.parse(d.toString())
var value = JSON.stringify((json.rate).toPrecision(7))
value = value.replace(/\"/g, "")
msg.reply(coin + ": $" + value);
});
});
request.end();
}
The server connection is working because if msg.content === 'money'
, it properly replies with nice
. If msg.content === 'BTC/USD'
, it does not reply.
It seems to not be changing the value of coin
before it makes the https.request
.
Any help is appreciated, thank you.