Displaying MySQL query results as HTML with EJS

问题: I am building an app (using EJS view engine and Node.js) which interacts with an MySQL database I have built locally. I can receive results from my queries successfully as...

问题:

I am building an app (using EJS view engine and Node.js) which interacts with an MySQL database I have built locally. I can receive results from my queries successfully as a JSON string, but I cannot send the information to a view (to be displayed as HTML) as I am getting the error: unsafefoods is not defined.

My 'app.js' file declares:

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

My route 'unsafefoods.js' declares:

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;

And my view 'unsafefoods.ejs' declares:

<!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>

Any pointers would be appreciated,

Thanks,


回答1:

unsafefoods variable is undefined in your unsafefoods.js file.

Your code should be

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

you are missing '' around unsafefoods.

  • 发表于 2018-07-05 19:03
  • 阅读 ( 289 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除