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.