We are developing a message board application where users can interact with each other. The application allows attachments with the messages. We are using stream.pipe(fs.createWriteStream) to upload the attachments.
We saw a strange behavior. If we post a message with attachment once, the application works fine. But, second time without page refresh the application fails to post.
There is no problem posting repeatedly without attachments. We are fairly new with node.js and looking for your comments.
Here is the code snippet for the message posting:`
if(messg.haveAttached==1){
ss(client).on('posting', function(stream, data5) {
var currentdate = new Date();
var currentTime = currentdate.getTime();
var filename = (data5.reactionFilePath)+ currentTime + "_" +path.basename(data5.name);
stream.pipe(fs.createWriteStream(filename)).on('finish', function() {
messg['attachedFilePath'] = filename;
Obj.save(messg,socketIOobj,client.id);
});
});
}else{
reactionData['attachedFilePath'] = '';
Obj.save(messg,socketIOobj,client.id);
}`
The error looks like:
.../node_modules/socket.io-stream/lib/iostream.js:97
this.socket._read(this.id, size); ^
TypeError: Cannot read property '_read' of null