我正在使用ssh2-sftp-client读取CSV文件并创建ReadableStream。然后,我尝试在库csvtojson的帮助下将此可读流转换为Json。但是,我总是收到以下错误:
TypeError:readStream.pipe不是函数
到目前为止,这是我的代码:
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');
});
有人知道我是否在正确地使用两个库,或者我尝试实现的目标是否不可能?
答案 0 :(得分:1)
由于sftp.get
返回了Buffer
,而您正试图将其传递给期望Stream
的函数,因此您收到错误消息。
将代码更改为此
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 asynchronousely
return new Promise(function(resolve, reject) {
resolve()
console.log(jsonObj);
})
})
}).catch((err) => {
console.log(err, 'catch error');
});