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();
});
});
}