Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda