Send Object Array to Bulkcreate

Asked

Viewed 265 times

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!

1 answer

0

I will try to translate for you quickly what arrived in the server, this here:

{
    '{"id":0,"idusuario":1,"idprofissional":1,"ativo":1},
{"id":0,"idusuario":1,"idprofissional":2,"ativo":1}': ''
}

It is nothing less than an object with a property called '{"id":0,"idusuario":1,"idprofissional":1,"ativo":1}, {"id":0,"idusuario":1,"idprofissional":2,"ativo":1}' with the value ''.

There are two problems in your method save, first here:

self.state.listProfissional.map((e, index) => {

And the other one here:

listAgenda.push(agenda);

The method map, works like this: It executes the function that you pass to it as argument, in each input of your array and it gives you three parameters, respectively, the value of the index, the number of the index and the array who is running the map. From what I’ve seen, you’ve already seen.

What you may not have noticed or not know is that, you have to return the value that you "mapped" at the end of the method you pass as an argument to the map. And another thing, when the map ends, it will return to you, a new array with mapped values.

So the right way for you to use this save would be more or less this one:

save() {
  const listAgenda = this.state.listProfissional.map(e => ({
    id: e.id,
    idusuario: e.idusuario,
    idprofissional: e.idprofissional,
    ativo: e.ativo
  }))
}

Ah and one more thing, avoid using self = this, This is unnecessary because there are already ways for you to pass the scope you want into a function block ok? In this case ai (probably no other), you don’t need to do this.

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.