I want to send a json document to a POST link route, on the backend, my API parses the json body with the bodyparser and inserts the document into a MongoDB Collection, however I always get "invalid json". When I am using w-xxx-urlencode and pass the fields with urlencode, it works normally. The json document should be valid itself:
{"_id": "5afa7fcb01b8c6e5a83aa8f6", "Timestamp":"2020-01-01T09:01:55.128Z","PB":9191,"pot":0,"nP":4343,"L":3333,"Lze":4444,"p":5555,"ne":66666666,"Pr":7777}
I get the error:
Error: invalid json
at error (C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:97:13)
at C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:50:63
at IncomingMessage.onEnd (C:\Users\U500405\git\Backend_node.js-Server\node_modules\raw-body\index.js:119:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
When I am leaving out the first "_id" field,I get the error:
POST /link/err 400 2.672 ms - 724
SyntaxError: Unexpected token T in JSON at position 1
at JSON.parse (<anonymous>)
at C:\Users\U500405\git\Backend_node.js-Server\node_modules\body-parser\index.js:52:25
at IncomingMessage.onEnd (C:\Users\U500405\git\Backend_node.js-Server\node_modules\raw-body\index.js:119:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
in the app.js module, I initialised to use Json bodyparser:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
My function to create the parsed json data and save in MongoDB is:
exports.create_an_FA= function(req, res) {
console.log(req.body)
var fa = new FA(req.body);
//SAVE the new instance
fa.save(function(err) {
if (err) {
console.log(err);
res.status(400);
res.send(err);
}
else {
console.log("Instanz FA in Datenbank erzeugt!");
res.status(200);
res.json({ message: 'FA-Instance created in datbase!' });
}
});
};
Why do I get the error no valid JSON data? it should switch automatically between json bodyparser x-www-urlencode and json(), depending on the body format right?
Edit:
I used the curl command to send post data as following:
curl -d "{\"Timestamp\":"\2020-01-01T09:01:55.128Z\","\PrognostizierterBetriebswert\":9191,"\posFlexPot\":0,"\negFlexPot\":4343,"\Leistungsuntergrenze\":3333,"\Leistungsobergrenze\":4444,"\posGesEnergie\":5555,"\negGesEnergie\":66666666,"\Preissignal\":7777}" -H "Content-Type: application/json" -X POST http://localhost:5000/DNZ/FA
and I get:
curl: (6) Could not resolve host: application
I tried without the escaping "\" character but then I get undefined or an empty document returned...
Related
I've been trying to send http reqeust by http node module by following code
const https = require('https')
http.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
but got the message
events.js:174
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:332:15)
at TLSSocket.socketOnEnd (_http_client.js:435:23)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at TLSSocket.socketOnEnd (_http_client.js:435:9)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
When I switch the code to
const request = require('request')
request.get('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
it worked and got a response with 200.
Then I try to capture network packages by Fiddler, I found no package was sent when using http.request.
This error only occurs in Windows system inside our working domain network.
maybe try https instead of http since you defined it https
https.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
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?
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!
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 ?
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.