0
votes

I was trying to write data into my .txt file. but an unhandled promise exception occurred.

I already added err statement in file.writeFileSyn but it seems like useless at all. How can I add promise part into the file.writeFileSync?

let file = fs
            .createWriteStream('depositeOrder.txt')
            .on('error', function(err) {
                console.log('[Writing error] : ' + err);
            });

        for (let content of readResult.entries()) {
            file.writeFileSync(content[0] + '|' + content[1], err => {
                console.log('We have an writing error');
                if (err) {
                    console.error(err);
                    return;
                }
            });
        }
        file.end();

This is the error message that I got

(node:21713) UnhandledPromiseRejectionWarning: TypeError: file.writeFileSync is not a function at getDeposite (/Users/jinwoopark/exkr-fds-service-2/apps/MonitorKrwDeposite.js:144:18) at process._tickCallback (internal/process/next_tick.js:68:7) warning.js:18 (node:21713) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

1

1 Answers

0
votes

I think the best way to approach this is to remember you're creating a file stream with the createWriteStream function. This does not have a writeFileSync method present.

Perhaps try this code:

let fileStream = fs
    .createWriteStream('depositeOrder.txt')
    .on('error', function(err) {
        console.error('[Writing error] : ' + err);
    });

for (let content of readResult.entries()) {
    fileStream.write(content[0] + '|' + content[1] + '\n');
}

fileStream.end();