0

In advance I have to say I'm new to node.js and may did not understand it fully yet.

I'm stuck at the moment with a rather obvious problem that I can't solve. I am importing a CSV file from a FTP server via the node module "ftp" then I'm receiving the file as a stream which I then convert to a JSON with the node module "csvtojson".

So far that works wonderfully. But now I noticed that I stumbled over some Umlaut chars from the German language which keep crashing my solution. I found out that I have to convert the file stream to UTF8, but I have no idea how.

I tried it already with the toString() method and several other node modules but that also didn't work.

My code looks like this:

getCsvFromFtp: function (profile) {
    init(profile);
    return new Promise((resolve, reject) => {
        client.on('ready', function () {
            client.get(file, function (err, stream) {
                if (err) {
                    return reject(err);
                }
                stream.once('close', () => {
                    client.end();
                });
                csv({
                    trim: true,
                    delimiter: delimiter,
                }).fromStream(stream, (err, result) => {
                    if (err) {
                        return reject(err);
                    }
                    return resolve(csvFormatterService.groupAndFormatOrders(result));
                });
            });
        });
        client.connect(ftpCredentials);
    });
}

EDIT: I found out that the problem was not my code, but the encoding of the file. the file was Western (ISO 8859-1) encoded.

Roger
  • 83
  • 1
  • 10

1 Answers1

0

You can use utf8 module from npm to encode/decode the string.

One more solution you could use is :

convertString = JSON.parse(JSON.stringify(convertString));

Hope this helps you.

MirzaS
  • 514
  • 3
  • 16
  • I tried it once with the second one you suggested. But I get this error: TypeError: Converting circular structure to JSON Now I try the module. I keep you updated. Thank you so far :) – Roger Aug 21 '17 at 08:28
  • You can look at this link https://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json for that problem, since the structure of your object is circular, or just stick with the utf8 module. – MirzaS Aug 21 '17 at 08:31
  • So I tried it now with the module. then I get as a result: k�chen Without it I get: k�chen And what I want to get is: küchen I'm not sure anymore if I'm on the right way with utf8 :S. – Roger Aug 21 '17 at 08:46
  • German characters usually work with UTF-8, since they use ISO/IEC 8859-15, you could try using UTF-16 or even UTF-32. – MirzaS Aug 21 '17 at 08:51
  • 1
    Alright. I solved it. after looking to all the files again (I almost got crazy) I realised that some of the files from another customer work. Reason: The files who dont work are ISO 8859-1 encoded ... so Im going to tell that one customer to encode his files in UTF-8 :P thank you – Roger Aug 21 '17 at 09:03