如何从node.js发送数组并在.ejs文件中渲染?

发布于 2025-02-05 05:30:43 字数 1571 浏览 1 评论 0原文

这是我第一次使用EJS& node.js ...

我的目标是发送一个数组并为每个元素制作一个IMGBOX元素。

这是我的node.js代码

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);
});

数组中制作元素(在这种情况下,名为“输出”的数组。


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

,这是我的.EJS代码,我想从 元素,但是屏幕上显示的东西只是字符串...

您能教我如何处理Node.js和EJS文件中的数组吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文