Separating elements from an array for sequelize query

Asked

Viewed 228 times

1

This id_message I send by parameter by my request, a console.log in this returns me the value of the image.

const id_mensagem = [...new Set(response.data.map(mensagens => mensagens.id_mensagem))]

inserir a descrição da imagem aqui

in my back I get this info this way:

const {id_mensagem}  = req.params

a console.log returns me 423,421,422.

However I need to pass this value between brackets for a query in the database, query code:

const mensagens = await Mensagens.findAll({
    where:{                    
        id_mensagem:{
            [Op.in]:[id_mensagem]
        }                                      
    },
    order: [['created_at','DESC']],
    
}) 

and here comes the problem, I’m using sequelize in this case and when he query and he searches only the first numbers before the comma, a console.log([id_message]) returns me this way: inserir a descrição da imagem aqui I understand that being wrapped in quotes the code is ignoring what comes after the first comma, finally how to turn this ['423,421,422'] into separate numbers for consultation! ex: [423,421,422].

2 answers

2


You are probably getting as a string "423,421,422". Then it would be necessary to split it in order to separate the values in an array:

const array = id_mensagem.split(',')

-1

In your query, try sending the reply message as a JSON, for example:

res.send({id_mensagem: id_mensagem});

That way he’ll send each id_message as an object and then you can access each other’s content from the front like this:

response.data.id_mensagem

Accessing each item separately and then being able to map.

I hope I’ve helped!

  • opa...thanks for the tip.

  • 1

    Hi Rafael, I think AP is having problems sending from the front to Node.js.

Browser other questions tagged

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