I am new to Node JS and I had done a lot of research and couldnt find any solution for the same. This is my code below
if(msg.body == 'Track ' + slug){
const str = msg.body;
const slug = str.substring(str.indexOf("Track") + 6); // 01-2020
var http = require('https');
var options = {
host: 'example.com',
path: '/example/example?id=' + slug
};
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('error', (err) => {
msg.reply('Error!');
})
response.on('end', function () {
var jsonObject = JSON.parse(str);
msg.reply('Current status of ' + slug + ': ' + jsonObject[0]['body']);
});
}
http.request(options, callback).end();
}
So if I enter Track and a value the value will be captured and then sent in the json request. The json request very much works unless there is an error where the app crashes. This becomes a very big problem. So If I enter the wrong value then the app crashes saying undefined in the log. I want it to msg.reply the error instead of the app crashing. Please help me out. Thank you in advance
var jsonObject = JSON.parse(str);andstrisn't JSON but is still the empty string? (for example if no data is sent with the request) Hint: you should have atry/catcharound this so you handle bad/missing input here.