0
I’m using Node to develop an application.
I am making a query in the bank to pull some values. These values are: 'imagem1', 'imagem2, 'imagem3'.
I want to use a for to apply a function in the 3 images:
const consulta = await conn.where({id: '10'}).select('*').table('cadastro')
for (let i = 1; i < 3; i++) {
console.log(`${consulta[0].imagem} ${i}`)
}
The constant query returns me what I want, however, the problem is time to print.
That console:
console.log(`${consulta[0].imagem} ${i}`),
undefined1, undefined2, undefined3, it does not concatenate. But if I do so:
console.log(`${consulta[0].imagem1}`)
console.log(`${consulta[0].imagem2}`)
console.log(`${consulta[0].imagem3}`)
everything happens correctly, bringing me the values.
It would not be good practice to print several times, because if I have 100 images, it will be a huge job. I need to use the for this, but the concatenation does not work, returns me 'Undefined'
Someone would have a solution to help me?
What is the result of
console.log(consulta)
?– Luiz Felipe
[ Rowdatapacket { id: 10, name: 'Marianna da', last name: 'Lemos', age: 2, imagem1: 'iPhone-12-5.jpg1610543032642.jpg', imagem2: 'iphone12-concept12-Scaled.jpg1610543032647.jpg', imagem3: 'Tr_iphone12_3d-1-2-920x613.png1610543032651.png' } ]
– Vitor Reis
He returns the data to me normally
– Vitor Reis
See if that’s what you’re trying to do:
console.log(consulta[0]['imagem' + i])
– bfavaretto
I think the ideal would be to place these images in an array inside the object, if it is possible to change the database
– Rafael Costa