I am using ssh2-sftp-client to read a CSV file and create a ReadableStream. Then I am trying to convert this readableStream to Json with the help of the library csvtojson. However, I always get the following error:
TypeError: readStream.pipe is not a function
Here is my code so far:
let Client = require('ssh2-sftp-client');
let sftp = new Client();
var csv = require("csvtojson");
sftp.connect({
host: 'HOST',
port: 'PORT',
username: 'USERNAME',
password: 'PASSWORD'
}).then(() => {
return sftp.get('/home/user/etc/testfile.csv');
}).then((data) => {
csv()
.fromStream(data)
.subscribe(function (jsonObj) { //single json object will be emitted for each csv line
// parse each json asynchronousely
return new Promise(function (resolve, reject) {
resolve()
console.log(jsonObj);
})
})
}).catch((err) => {
console.log(err, 'catch error');
});
Does anyone know if I am using both libraries correctly or whether it is not possible what I try to achieve?
You are getting an error because sftp.get
returns a Buffer
and you're trying to pass it to a function that expects a Stream
.
Change your code to this
let Client = require('ssh2-sftp-client');
let sftp = new Client();
var csv = require("csvtojson");
sftp.connect({
host: 'HOST',
port: 'PORT',
username: 'USERNAME',
password: 'PASSWORD'
}).then(() => {
return sftp.get('/home/user/etc/testfile.csv');
}).then((data) => {
csv()
.fromString(data.toString()) // changed this from .fromStream(data)
.subscribe(function(jsonObj) { //single json object will be emitted for each csv line
// parse each json asynchronously
return new Promise(function(resolve, reject) {
resolve()
console.log(jsonObj);
})
})
}).catch((err) => {
console.log(err, 'catch error');
});