Send Array of objects to Bulkcreate

0

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,     

asked by anonymous 03.01.2018 / 14:01

1 answer

0

I will try to translate to you quickly what arrived at 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 value '' .

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

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

And the other one here:

listAgenda.push(agenda);

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

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

So the right way to use this save would be something like this:

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

Oh and one more thing, avoid using self = this , this is unnecessary because there is already a means of passing the scope that you want into a function block ok? In this case there (probably nowhere else), you do not have to do that.

    
05.01.2018 / 04:12