【问题标题】:Cannot render MySQL data using ExpressJS and EJS无法使用 ExpressJS 和 EJS 呈现 MySQL 数据
【发布时间】:2021-03-27 13:08:59
【问题描述】:

我无法在 HTML 页面上显示 MySQL 表数据。现在,我正在使用带有 EJS 模板引擎的 Express 框架。 我有app.js 主文件和index.ejs 文件(在views 文件夹上)。 我有一个简单的表,名称为:表单和两个字段(名称、用户名)。 运行应用程序时,请参阅以下错误:

TypeError: C:\Users\azeri\Desktop\NODE-COURSE\node-form\views\index.ejs:11
    9|           </thead>  

    10|            <tbody>  

 >> 11|             <% obj.forEach(function(user){ %>

    12|                 <tr>  

    13|                     <td><%= user.name %></td>  

    14|                     <td><%= user.username %></td>


obj.forEach is not a function
    at eval (C:\Users\azeri\Desktop\NODE-COURSE\node-form\views\index.ejs:10:12)
    at index (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\ejs\lib\ejs.js:691:17)
    at tryHandleCache (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\ejs\lib\ejs.js:272:36)
    at View.exports.renderFile [as engine] (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\express\lib\response.js:1012:7)
    at Query.<anonymous> (C:\Users\azeri\Desktop\NODE-COURSE\node-form\app.js:23:11)
    at Query.<anonymous> (C:\Users\azeri\Desktop\NODE-COURSE\node-form\node_modules\mysql\lib\Connection.js:526:10)

app.js 文件

var express = require('express');
var app = express();
var path = require('path');
var mysql = require('mysql');

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "nodeform"
});

var obj = {};
app.get('', function(req, res){
  con.query("SELECT * FROM form", function (err, result) {
    if (err) throw err;
    else {
      obj = {print: result};
      res.render('index', {obj: obj});
    }
  });
});

app.listen(3000);
console.log("Running at Port 3000");

index.ejs 文件

<html>
      <body>
        <table id="table">  
          <thead>  
              <tr>  
                  <th>Name</th>  
                  <th>Username</th>  
              </tr>  
          </thead>  
           <tbody>  
            <% obj.forEach(function(user){ %>
                <tr>  
                    <td><%= user.name %></td>  
                    <td><%= user.username %></td>
                </tr>                                   
            <% }) %>
           </tbody>
      </table>
    </body>
</html>  

【问题讨论】:

    标签: mysql node.js express ejs


    【解决方案1】:

    这里的问题是你的 obj 对象有一个子属性 print 是结果数组,所以你应该在你的 EJS 中的 obj.print 上使用 forEach,因为 forEach 只适用于数组:

    <tbody>
        <% obj.print.forEach(function(user){ %>
            <tr>  
                <td><%= user.name %></td>  
                <td><%= user.username %></td>
             </tr>                                   
        <% }) %>
    </tbody>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 2014-04-14
      • 2021-12-18
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多