Even though I can clearly see my output in console. I can't write them into a file. There is 1 output file and its undefined-03-02-2017.txt which contains single line '15:33 undefined'.
for (i = 0; i < channelcount; i++) {
    messages[i] -= messagesOld[i];
    console.log(channels[i] + ' ' + messages[i]);
    messages[i] = messagesOld[i];
    fs.open('logs/' + channels[i] + '.txt', 'r', function (err, fd) {
        if (err && err.code == 'ENOENT') {
            fs.writeFile('logs/' + channels[i] + '-' + moment().format('MM-DD-YYYY') + '.txt', moment().format('H:mm') + ' ' + messages[i], function (err) { });
        } else {
            fs.appendFile('logs/' + channels[i] + '-' + moment().format('MM-DD-YYYY') + '.txt', moment().format('H:mm') + ' ' + messages[i] + '\n', function () { });
        }
    });
}
 
     
    