如何用ejs将MySQL查询结果显示为HTML?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (202)

我正在构建一个应用程序(使用ejs、视图引擎和Node.js),它与我在本地构建的MySQL数据库进行交互。我可以JSON字符串的形式成功地从查询中接收结果,但当我收到错误时,无法将信息发送到视图(将显示为HTML):未定义非安全食品。

我的‘app.js’文件声明:

var unsafefoods = require('./routes/unsafefoods');
app.use('/unsafefoods', unsafefoods);

我的路径“不安全食品”声明:

var express = require('express');
var router = express.Router();

/* GET all safe foods */
router.get('/', function (req, res, next) {
	connection.query('SELECT * from appycavy.foods WHERE safe = 0', function (error, rows, fields) {
		if (error) {
			res.send(JSON.stringify({
				"status": 500,
				"error": error,
				"response": null
			}));
			//If there is error, we send the error in the error section with 500 status
		} else {
			res.render(unsafefoods, {
				title: 'Unsafe foods list',
				data: rows
			})
		}
	});
});

module.exports = router;

我的观点“非安全食品”声明:

<!DOCTYPE html>
<html lang="en">

<head>
  <% include ./partials/head %>
</head>

<body class="container">
  <header>
    <% include ./partials/header %>
  </header>
  <main>
    <div class="jumbotron">
      <h2>Safe foods</h2>
      <p>The following is a list of all
        <b>unsafe</b> foods
      </p>
      <!-- table to display unsafe foods -->
      <table width='80%' border=0>
 
        <tr style='text-align:left; background-color:#CCC'>
            <th>ID</th>
            <th>Name</th>
            <th>Safe</th>
            <th>Type</th>
            <th>Favourite</th>
            <th>Water</th>
            <th>Energy</th>
            <th>Vitamin C</th>
            <th>Sugar</th>
            <th>Lipid fat</th>
            <th>Calcium</th>
            <th>Phosphorus</th>
            <th>CaP ratio</th>
        </tr>
        
        <!--
            Using FOREACH LOOP for the users array
            
            myArray.forEach(function(el, index) {
                // el - current element, i - index
            });
        -->
        <% if (data) { %>
        <% data.forEach(function(unsafefoods){ %>
            <tr>
                <td><%= unsafefoods.id %></td>
                <td><%= unsafefoods.name %></td>
                <td><%= unsafefoods.safe %></td>
                <td><%= unsafefoods.type %></td>
                <td><%= unsafefoods.favourite %></td>
                <td><%= unsafefoods.water %></td>
                <td><%= unsafefoods.energy %></td>
                <td><%= unsafefoods.vitaminC %></td>
                <td><%= unsafefoods.sugars %></td>
                <td><%= unsafefoods.lipidFat %></td>
                <td><%= unsafefoods.calcium %></td>
                <td><%= unsafefoods.phosphorus %></td>
                <td><%= unsafefoods.capRatio %></td>
                <td>
                    <div style="float:left">
                        <a href='/unsafefoods/edit/<%= unsafefoods.id %>'>Edit</a> &nbsp;                            
                        <form method="post" action="/unsafefoods/delete/<%= unsafefoods.id %>" style="float:right">
                            <input type="submit" name="delete" value='Delete' onClick="return confirm('Are you sure you want to delete?')" />
                            <input type="hidden" name="_method" value="DELETE" />
                        </form>
                    </div>
                </td>
            </tr>
        <% }) %>
        <% } %>
     
    </table>
    </div>
  </main>
  <footer>
    <% include ./partials/footer %>
  </footer>
</body>

</html>

任何指点都会很感激,

谢谢

提问于
用户回答回答于

unsafefoods变量在unsafefoods.js文档

你的代码应该是

res.render('unsafefoods', {
  title: 'Unsafe foods list',
  data: rows
})

扫码关注云+社区

领取腾讯云代金券