0

I am trying to parse a csv file from the FTP server. The problem arises when the file has only one line of data. It works fine if the file has more than 1 line of data. The following code does the job. After debugging I figured out that fromString method does not return anything, However, the data from the socket is read successfully. Any help is appreciated

async function getFileData(ftpConnection, filename) {
  return new Promise((resolve, reject) => {
    ftpConnection.get(filename, (err, socket) => {
      const products = [];
      if (err) {
        console.log(err)
        reject(null);
      }
      socket.on('data', data => {
        csv({
          headers: HEADERS,
          delimiter: [';'],
        })
        .fromString(data.toString('latin1'))
        .subscribe((product) => {
          // Product is empty when its only one line
          products.push(product);
        })
      });

      socket.on('close', closingError => {
        if (closingError) {
          console.error('[Error]: ', filename);
          reject(null)
        } else {
          console.log("PRODUCTs")
          console.log(products)
          resolve(products);
        }
      });
      socket.resume();
    });
  });
}
2
  • maybe you can try to append an empty line or just a new line char \n. Something like .fromString(data.toString('latin1') + '\n') Commented Dec 18, 2019 at 9:18
  • atleast appending a new line character does not work :( Commented Dec 18, 2019 at 9:22

2 Answers 2

1

By default csv2json assumes you have a header describing your csv file. If you have just one line this line is considered to be the header and therefore you have no data.

You can avoid this behavior by setting the noheader option to true.

csv ( {noheader:true} ).fromString ('1,2,3').then ((csv) => { console.log (csv); });
Sign up to request clarification or add additional context in comments.

Comments

0
const CSV2JSON = async(dumb, editDumb, headers) => {
    try {
        log(`\n\nStarting CSV2JSON - Current directory: ${__dirname} - Please wait..`)

        if (!dumb && !editDumb && !headers) {
            var {dumb, editDumb, headers} = require('minimist')(process.argv.slice(2))
        }

        const options = {
            trim: true,
            delimiter: '|',
            quote: '\'',
            escape: '\'',
            fork: true,
            // output: "csv",
            // eslint-disable-next-line max-len
            headers: Array.isArray(headers) ? headers : JSON.parse(headers)
            // headers: headers,
            // noheader: true
        }

        await new Promise((resolve, reject) => {
            let counter = 0
            lineReader.eachLine(dumb, async(line, last) => {
                log(line)
                counter++

                let json = await csv(options).fromString(headers + '\n\r' + line)

                json = JSON.stringify(json[0])

                if (counter === 1) {
                    // Check for first Line
                    json = `[${json},`
                } else if (last) {
                    // Check for last Line
                    json = `${json}]`
                    resolve()
                } else {
                    // Check for inbetween Line
                    json = `${json},`
                }

                await fs.appendFile(editDumb, json)
            })
        })
    } catch (e) {
        throw new BaseError(`Error while converting CSV to JSON - Error: ${e}`)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.