带有把手的Express-res.render不起作用

时间:2018-12-22 20:59:34

标签: javascript node.js

当前,我正在尝试创建一个小的数据库查询,在其中输入名称,然后获取包含返回数据的表。 我的问题是,当我尝试使用res.render渲染数据时,它什么都不做。没什么改变。

我的NodeJS文件:

const mysql = require('mysql');
const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
const sql = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "samp"
});
const exphbs = require("express-handlebars");

sql.connect(function(err) {
    if (err) throw err;
});

app.use((req, res, next) => {
    res.append('Access-Control-Allow-Origin', ['*']);
    res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.append('Access-Control-Allow-Headers', 'Content-Type');
    next();
});
app.use(express.static("css"));
app.use(express.static("js"));
app.use(express.static("views"));

app.engine("handlebars", exphbs({ defaultLayout: "main", layoutsDir: __dirname + "/views/layouts/" }));
app.set("view engine", "handlebars");
app.set("views", path.join(__dirname, "views"));

app.get("/mysql", (req, res) => 
{
    let query = req.query.query;
    if(query === null || query === undefined) return res.render("mysql");
    if(query.length == 0) return res.render("mysql", {error: "The query cannot be empty."});
    console.log("SELECT * FROM `players` WHERE `Name` LIKE '%" + query + "%'");

    sql.query("SELECT * FROM `players` WHERE `Name` LIKE ?", "%" + query + "%", (err, result, fields) =>
    {
        if(err) throw err;
        if(result.length == 0)
        {
            console.log("error");
            return res.render("mysql", {error: "The query returned nothing."});
        }
        let arr = [];
        Object.keys(result).forEach(function(key) 
        {
            let row = result[key];
            arr.push({ID: key, Name: row.Name, LastLogin: row.LastLogin, Admin: row.Admin});
            console.log(arr);
        });

        let data = 
        {
            items: arr
        }
        res.render("mysql", data);
        console.log(data);
    });

});

app.get("/", (req, res) =>
{
    res.render("root", {number: 0});
});

app.use((req, res, next) =>
{
    res.status(404);
    res.render("404", {url: req.url});
});

app.listen(port);
console.log("Der Hauptserver wurde erfolgreich gestartet!");

还有我的把手文件(mysql.handlebars):

<script>
  $(document).ready(function(){
    let query;
    $("#submit").click(function(){
      query=$("#query").val();
      $.get("http://localhost:3000/mysql/",{query: query}, function(data)
      {
      });
    });
  });
</script>
  <h1 class="display-4">MySQL Query with Node.js Express</h1>
   
  <input type="TEXT" id="query" size="40"><br>
  <input type="button" id="submit" value="Execute">

  {{#if error}}
  <p class="lead mysqlerror">{{error}}</p>
  {{/if}}

  {{#if items}}
  <table class="table">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Letzter Login</th>
      <th scope="col">Adminlevel</th>
    </tr>
  </thead>
  <tbody>
    {{#each items}}
      <tr>
        <td>{{ID}}</td>
        <td>{{Name}}</td>
        <td>{{LastLogin}}</td>
        <td>{{Admin}}</td>
      </tr>
    {{/each}}
  </tbody>
  </table>
  {{/if}}

正确的调试日志出现在我的nodejs终端中,但显示为res.render却什么也不做,并且我提供的数据也没有出现在我的网页上。

我尝试了很多事情,但似乎无法正常工作。

希望这里有人知道如何解决此问题。

0 个答案:

没有答案