Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!