如何将数据库内容显示到ejs网页?

lo8azlld  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(486)

所以我有一个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,因为我正在尝试查看信息是否正在传递。然而,它并没有,它说承诺已经解决了。那么,如何在ejs页面上显示db内容呢。我想做一个网上商店,我想知道我会怎么做。感谢您的帮助!
在一些帮助下,我能够将数据库内容获取到store.js,但仍然无法将内容从store.js获取到store.ejs页面。

bejyjqdl

bejyjqdl1#

如果要向ejs传递一个用于渲染的承诺,则需要传递值。更改呈现函数以解析产品承诺并将结果转发给ejs。例如:

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

根据每个请求,从db处获得产品:

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

相关问题