0

I'm using express 4.16.

app.use(express.json());
...
router.post('/add', function (req, res, next) {
    var course = req.body;
    console.log(course);
});

the result looks like:

 { 
  course_name: 'xxx',
  course_start_date: '01-02-2019',
  course_end_date: '15-02-2019',
  'days[0][dayIdx]': '1',
  'days[0][from]': '16:45',
  'days[0][to]': '17:00',
  'days[1][dayIdx]': '2',
  'days[1][from]': '17:45',
  'days[1][to]': '17:30' 
 }

what i'm looking for is having the following:

 { 
  course_name: 'xxx',
  course_start_date: '01-02-2019',
  course_end_date: '15-02-2019',
  'days':[
      {'dayIdx': '1', 'from': '16:45', 'to': '17:00'},
      {'dayIdx': '2', 'from': '17:45', 'to': '17:30'}
    ] 
 }

the input I send to the node is already contains an Array: enter image description here

any elegant way or a library rather than iterating the keys and create the arrays manually?

Gil Peretz
  • 2,399
  • 6
  • 28
  • 44
  • 1
    The elegant solution is to _send_ the data in the correct format provided you write the server code. – marekful Feb 01 '19 at 15:18
  • @marekful - already did. I've update my question with screenshot of the request data – Gil Peretz Feb 01 '19 at 15:24
  • Can you please add the request headers you are sending? Specifically the `content-type` of the request. – Alex Michailidis Feb 01 '19 at 15:29
  • I didn't pass any headers... when I do pass application/json, than the request failed. I assume it because something in express.json() - I don't get any stacktrace, just '400' as status code @alex-rokabilis – Gil Peretz Feb 01 '19 at 15:50
  • 1
    Is it possible that you are sending `formdata`? Can you post the full code that you are using to send the request? Also try with postman to see if the error remains. – Alex Michailidis Feb 01 '19 at 16:10
  • Try to set the "extended : true" option at the json parser. https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0 – ykit9 Feb 01 '19 at 21:28

0 Answers0