I am trying to create record from a form data. When I console.log(req.body) I get the following record.
req.body => [Object: null prototype] {
id: '1',
key1: 'Value1',
key2: 'Value2',
supervisors: '[object Object],[object Object],[object Object]'
}
So I checked the database where the record is stored and see that supervisors is stored as:
supervisors: Array(3)
0:
name: "Reporter"
userId: 4
1:
name: "Officer 1"
userId: 5
2:
name: "Coordinator"
userId: 2
I will like to get the values of userId as an array in supervisors field in the req.body so that my req.body will look like:
req.body => [Object: null prototype] {
id: '1',
key1: 'Value1',
key2: 'Value2',
supervisors: '[4, 5, 2]'
}
I did
const supervisors = JSON.stringify(req.body.supervisors))
and I got[object Object],[object Object],[object Object]
when console loggedI did
const supervisors = q.supervisors ? JSON.parse(q.supervisors) : [];
and I gotSyntaxError: Unexpected token o in JSON at position 1 at JSON.parse
when console logged.I did
const supervisors = req.body.supervisors.map(sup => sup.userId);
and I gotreq.body.supervisors.map is not a function
when console logged.
How can I get the supervisors value as [2, 4, 5]?