0
ストリーミングからのデータを処理するときに、未定義の「トリム」私はストリーミングから以下のデータを持っている:NodejsとはTypeError方法
'ID|20120206|080500|0000001|0|1|record1|END'
'ID|20120206|080500|0000002|0|1|record2|END'
'ID|20120206|080500|0000003|0|1|record3|END'
と私はnodejsに以下のコード使用して線でストリーミングデータラインを処理したい:
var net = require('net');
var HOST = 'localhost',
PORT = 9010,
lastSeqNo = 0;
var client = new net.Socket();
client.setEncoding('ascii');
client.connect(PORT, HOST, function() {
console.log('Connected to: %s : %d', HOST, PORT);
});
client.on('close', function (hadError) {
console.log(hadError);
});
client.on('error', function (ex) {
console.log(ex);
});
var i = 1;
var Line = function (rows, data) {
this.SeqNo = parseFloat(rows[3].trim());
this.Date = rows[1].trim();
this.Time = rows[2].trim();
this.MsgType = parseInt(rows[4].trim(), 10);
this.Data = data;
};
client.on('data', function (data) {
var content = data.split('\r\n');
content.forEach(function (item) {
if (item.length > 0) {
console.log(i, item);
i++;
var rows = item.split('|'),
line = new Line(rows, data),
seqNo = line.SeqNo,
msgType = line.MsgType;
console.log('seqno:', seqNo);
}
});
});
を
は、いくつかのデータライン処理の後、私は以下のようないくつかのエラーを得た:
D:\test node\app.js:33
this.SeqNo = parseFloat(rows[3].trim());
^
TypeError: Cannot call method 'trim' of undefined
at new <anonymous> (D:\test node\app.js:33:37)
at D:\test node\app.js:48:24
at Array.forEach (native)
at Socket.<anonymous> (D:\test node\app.js:42:13)
at Socket.emit (events.js:67:17)
at TCP.onread (net.js:362:31)
あなたは私を助けることができる何を上記のコードで間違っていますか?
ありがとうございました。インドネシアからの挨拶
@ jani-hartikainenありがとうございました。あなたの返信を読んだ後に私のコード変更の下で: 私は完全にソケットからclient.on 'データ'を削除し、これと置き換えます。 var carrier = require( 'carrier'); var my_carrier = carrier.carry(クライアント); my_carrier.on( 'line'、function(line){ if(line.length> 0){ var rows = line.split( '|')、 aLine =新しい行(行、行)、 seqNo = aLine.SeqNo、 msgType = aLine.MsgType; console.log( '行:'、行); } }); – hbinduni