Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
How to send an array from node.js and render it in .ejs file?

This is my first time for using ejs&node.js...

My goal is sending an array and making an imgBox element for each element.

Here is my node.js code

const express = require('express');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3');
const sqlite = require('sqlite');

const app = express();
app.set('view engine','ejs');
app.set('views','./views');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(express.static(__dirname+'/public'));

async function getDBConnection(){
    const db = await sqlite.open({
        filename : 'product.db',
        driver: sqlite3.Database
    });
    return db;
}

app.get('/', async function(req,res){
    let db = await getDBConnection();
    let rows = await db.all('select * from items');
    await db.close();   
    
    var output=new Array();

    for(var i=0;i<rows.length;i++){
        output.push(rows[i]['product_title']);
    }
    
    res.render('index',{
        test: "ㅇㅇ ",
        title: output
    });
});

var port = 3000;
app.listen(port,function(){
    console.log('server on! http://localhost: '+port);
});

and here is my .ejs code where I want to make elements from an array(in this case, the array named as 'output'.):


            <div id="itemBox">
                <% var len = title.length; %>
                <% for(let i=0;i<len;i++){ %>
                    <div id="imgBox"><%=title[i]%></div><br>
                <% } %>    
            </div>

What I wanted is making (len) imgBox elements by array's elements, but the thing displayed on the screen was only string...

Could you please teach me how to deal with an array in node.js and ejs file?

Thanks.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda