Node.js csv-parse error mystery using Sails - node.js

It processes most of the records from the .csv file. However, at the end I get this error with csv-parse node module and can't figure out why. It terminates my server so I need to resolve it. Please help
events.js:160
throw er; // Unhandled 'error' event
^
Error: Number of columns on line 37052 does not match header
at Error (native)
at Parser.__push (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:222:13)
at Parser._flush (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:189:12)
at Parser.<anonymous> (_stream_transform.js:118:12)
at Parser.g (events.js:291:16)
at emitNone (events.js:86:13)
at Parser.emit (events.js:185:7)
at prefinish (_stream_writable.js:504:12)
at finishMaybe (_stream_writable.js:512:7)
at endWritable (_stream_writable.js:524:3)
at Parser.Writable.end (_stream_writable.js:489:5)
at ReadStream.onend (_stream_readable.js:511:10)
at ReadStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at ReadStream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
My Controller API
upload: function(req, res){
req.file('statData').upload(function(error, files){
var fs = require('fs');
var parse = require('csv-parse');
var csvData=[];
var lines = 0;
var filePath = sails.config.appPath + '/data/file.csv';
fs.createReadStream(filePath)
.pipe(parse({
columns: false,
delimiter: '\t',
skip_empty_lines: true,
relax: true
}))
.on('data', function(csvrow) {
csvData.push(csvrow);
})
.on('end',function() {
//do something wiht csvData
});
}

I switched to using fast-csv module and worked.

Related

Error [ERR_STREAM_WRITE_AFTER_END]: write after end ExpressJS

I forked a repo I found and running it locally was fine with no errors however I wanted to run it locally on https so I changed the code of src/index.js
const server = createServer();
to
const options = {
key: fs.readFileSync('localhost-key.pem'),
cert: fs.readFileSync('localhost.pem')
};
const server = https.createServer(options, app)
But I started getting the error after the change when I try to access the website on https://localhost:8080
Full Error:
node:events:491
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:399:5)
at write_ (node:_http_outgoing:868:11)
at ServerResponse.write (node:_http_outgoing:827:15)
at ReadStream.ondata (node:internal/streams/readable:766:22)
at ReadStream.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at node:internal/fs/streams:275:14
at FSReqCallback.wrapper [as oncomplete] (node:fs:681:5)
Emitted 'error' event on ServerResponse instance at:
at ServerResponse.onerror (node:internal/streams/readable:785:14)
at ServerResponse.emit (node:events:525:35)
at emitErrorNt (node:_http_outgoing:846:9)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}

Node Slack API file upload saying no_file_data when using fs.createReadStream

The following code:
const Slack = require('slack');
const fs = require('fs');
const token = 'super-secret-token-my-brah';
const slack = new Slack({ token });
slack.files.upload({
channel: 'general',
file: fs.createReadStream(__dirname + '/test.jpg'),
})
.then(console.log)
.catch(console.error);
Is giving me the following error:
Error: no_file_data
at _res (/Users/myname/Desktop/repos/myapp/node_modules/slack/src/_exec.js:56:17)
at IncomingMessage.__end (/Users/myname/Desktop/repos/myapp/node_modules/tiny-json-http/_write.js:75:9)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
The file is present at the path given, so that's not the problem. I've been searching through the Slack documentation and everything, but no leads.
Anyone have any idea what's happening here?

Can't set headers after they are sent when trying to redirect after using Google's oauth2Client.getToken()

After receiving oauth2 tokens from google I am trying to redirect to an endpoint on my express server to make api requests.
This is the flow of the auth requests:
app.get('/api/presentation/getpresentation/:id', function(req, res, next){
req.session.user = req.user.id;
req.session.presentation_id = req.params.id;
res.redirect(google_auth_url)
});
app.get('/oauth2callback', function(req, res) {
const code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
req.session.tokens = tokens;
if (!err) {
oauth2Client.setCredentials(tokens);
res.redirect(`/api/presentation/${req.session.presentation_id}`);
return;
}
res.status(500).send(err);
})
});
app.get('/api/presentation/:id', **do things with api consent**);
The error:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:767:10)
at ServerResponse.location (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:884:15)
at ServerResponse.redirect (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:922:18)
at /Users/joe/school/solo-project/present/server/index.js:112:11
at /Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/auth/oauth2client.js:154:5
at Request._callback (/Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/transporters.js:106:7)
at Request.self.callback (/Users/joe/school/solo-project/present/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1057:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Any help is greatly appreciated, I've been tearing my hair out on this one for a while!
Thanks!

Node.js 'socket hang up' and keep-alive

I have a very strange error happening.
I am running Node.js version 8.0.0 on Mac OS X Sierra.
I create a very simple http server that basically returns 'ok' to any request after 8 seconds in this example.
I then use the 'request' package to make a request to my http server every 9 seconds, and use the parameter 'forever' to keep the http session alive.
One request out of two works perfectly fine while the other out of two returns a 'socket hang up' error.
Here is the very simple code to reproduce the issue:
const http = require('http');
const request = require('request');
const port = 3000;
const server = http.createServer((req, res) => {
console.log('request');
setTimeout(() => {
res.end('ok');
}, 8000);
});
server.listen(port);
setInterval(() => {
request(`http://localhost:${port}/`, {forever: true}, (error, response, body) => {
if (error) return console.log('error', error);
console.log('done', body);
});
}, 9000);
When I run this code I get the following output on my Macbook Pro :
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
So I get an 'socket hang up' error exactly every other request.
It seems that the error does not seem to appear on an Ubuntu VPS though.
Any ideas as to why this issue is happening ?

Why does my restify JSON client not work?

I'm following the example here
http://mcavage.github.com/node-restify/#JsonClient
As follows
var client = restify.createJsonClient({
url: 'http://api.stackoverflow.com'
});
client.get('/1.1/users/19068', function(err, req, res, obj) {
assert.ifError(err);
console.log('%j', obj);
});
I'm getting the following error message
SyntaxError: Unexpected token
at Object.parse (native)
at /.../node_modules/restify/lib/clients/json_client.js:54:20
at IncomingMessage.<anonymous> (/.../node_modules/restify/lib/clients/string_client.js:149:16)
at IncomingMessage.g (events.js:156:14)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1147:24)
at TCP.onread (net.js:354:27)

Resources