My answer returns Undefined

Asked

Viewed 40 times

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)?

  • [ 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' } ]

  • He returns the data to me normally

  • 1

    See if that’s what you’re trying to do: console.log(consulta[0]['imagem' + i])

  • 1

    I think the ideal would be to place these images in an array inside the object, if it is possible to change the database

No answers

Browser other questions tagged

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