gpt4 book ai didi

javascript - 如何将数据库内容显示到EJS网页?

转载 作者:行者123 更新时间:2023-11-29 17:16:01 25 4
gpt4 key购买 nike

所以我有一个包含以下代码的 JavaScript 页面:Story.js:

var express = require('express');
var dbcon = require('./app/db/databaseconnection');

var app = express();
var router = express.Router();
dbcon.createTable();
var filepath = __dirname + '/views/';
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use('/', router);

var results = dbcon.getProducts().then(results => {
return results;
}).catch(err => { console.log(err) })

router.get('/', (request, response) => response.render(filepath + 'index', { page_name: 'home' }));
router.get('/store', (request, response) => response.render(filepath + 'store', { page_name: 'store', products: results }));
router.get('/about', (request, response) => response.render(filepath + 'about', { page_name: 'about' }));
router.get('/contact', (request, response) => response.render(filepath + 'contact', { page_name: 'contact' }));
router.get('/build', (request, response) => response.render(filepath + 'build/build'));
router.get('/learn', (request, response) => response.render(filepath + 'learn/learn'));
app.use('*', (request, response) => response.render(filepath + '404', { page_name: '404' }));
app.listen(3000, () => console.log("Server running at Port 3000"));

我还有一个包含数据库内容的 JavaScript 页面:Databaseconnection.js:

const mysql = require("mysql");

const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "root",
password: "LOTOS123l",
database: "dbComputerStore"
});

module.exports = {
getProducts: () => new Promise((resolve, reject) => {
pool.query("SELECT * FROM products", (error, results, fields) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
}),
createTable: () => new Promise((resolve, reject) => {
PRIMARY KEY NOT NULL, image LONGBLOB NOT NULL)", (error, results, fields) => {
pool.query("SELECT * FROM pimages", (error, results, fields) => {
if (error) {
reject(error);
} else {
resolve(results);
console.log(results);
}
});
}),
populatetable: () => new Promise((resolve, reject) => {
pool.query();
})
};

然后我有我的 ejs 页面,我试图将其显示到:store.ejs

<!DOCTYPE html>
<html>

<head>
<title>PC Store | Store</title>
<% include partials/header %>
</head>

<body>
<header>
<% include partials/navbar %>
</header>
<main>
<div class="container">
<% console.log(products); %>
</div>
</main>
<footer>
<% include partials/footer %>
</footer>
</body>

</html>

目前,我将其设置为 console.log,因为我试图查看信息是否正在传递。然而事实并非如此,它说“Promise Resolved”。那么我如何将我的数据库内容显示到 ejs 页面。我正在尝试创建一家在线商店,我想知道如何做到这一点。任何帮助表示赞赏!

在一些帮助下,我能够将数据库内容获取到 store.js,但我仍然无法将内容从 store.js 获取到 store.ejs 页面。

最佳答案

您将 promise 传递给 EJS 进行渲染,您需要传递该值。更改渲染函数以解决产品 promise 并将结果转发给 EJS。例如:

router.get('/store', (request, response) => {
return productPromise.then(results => {
return response.render(filepath + 'store', { page_name: 'store', products: results });
});
});

或者根据每个请求从数据库获取产品:

router.get('/store', (request, response) => {
return dbcon.getProducts().then(results => {
return response.render(filepath + 'store', { page_name: 'store', products: results });
});
});

关于javascript - 如何将数据库内容显示到EJS网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51622615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com