0
I’m using Node JS (backend) with sequelize 4.4.2 and I need to send an array of objects from my front (React JS) but I can’t generate in the format that is expected, only to comment when I did a test (hardcode) it worked inserted the data.
Expected array:
const listValues = [
{id: 0, idusuario: 2, idprofissional: 1, ativo: 1},
{id: 0, idusuario: 2, idprofissional: 2, ativo: 1}
];
I have tried JSON.parse, JSON.stringify, both on the Front and mainly on the back, I believe that due to the lack of full knowledge in Arrays, JSON, these formats I am missing something.
Result that arrives in 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 to the 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 listAgenda is sent to the backend, but when viewing through the console I noticed that the format changes, I have the following settings and have tried to change, comment but nothing helps, thinking that the backend is missing something:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
Route in 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 have tried to send through string, type mount format, but gave error!
Jan Cassio thank you very much for the help, but I continue with the problem, do not enter the data, I made the changes that guided me, but the request (req.body) to my backend as I need, in case an Array of Objects, if I add JSON.stringify(listAgenda) arrives on the back but gives error, without the stringify the req.body gets ['',''] I’m trying thanks again
– CÉSAR SANTOS