I am using Node JS (backend) with sequelize 4.4.2 and need to send an array of objects from my front (React JS) but I can not generate in the format that is expected, just to comment when I did a test (hardcode) it worked fine inserted the data.
Expected Array:
const listValues = [
{id: 0, idusuario: 2, idprofissional: 1, ativo: 1},
{id: 0, idusuario: 2, idprofissional: 2, ativo: 1}
];
I've tried JSON.parse, JSON.stringify, both in the Front and mostly in the back, I believe because of the lack of full knowledge in Arrays, JSON, these formats I'm missing something.
Result that arrives in the BackEnd:
{ '{"id":0,"idusuario":1,"idprofissional":1,"ativo":1},{"id":0,"idusuario":1,"idprofissional":2,"ativo":1}': '' }
Part of the code that generates the array and sends it back:
save() {
let self = this;
let listAgenda = [];
self.state.listProfissional.map((e, index) => {
let agenda = {};
agenda.id = e.id;
agenda.idusuario = e.idusuario;
agenda.idprofissional = e.idprofissional;
agenda.ativo = e.ativo;
listAgenda.push(agenda);
});
...
This list is sent to the backend, but when viewing by the console I noticed that the format changes, I have the following settings and I tried to change, comment but nothing works, thinking that the backEnd is missing something:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
Route in the backEnd responsible for insertion:
app.route("/agendaprofissional/insertupdate")
.all(app.auth.authenticate())
.post((req, res) => {
let options = {validate: true, individualHooks: true};
console.log("req.body: ", req.body);
AgendaProfissional.bulkCreate(req.body, options)
.then(result => {
if(result){
res.json(result);
}
else{
res.sendStatus(404);
}
})
.catch(error => {
res.status(412).json({msg: error.message});
});
})
In short, this is if someone can guide me, show the error, how to get the result that worked, I already tried sending the string, type the format,