如何共享 Node.js 中的所有 mysql 行? (车把)
How to can I share all of mysql rows in Node.js? (handlebars)
我用这个命令共享第一行 (jack)。
节点:
const express = require("express");
const app = express();
app.set("view engine","hbs")
const mysql = require("mysql")
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "stack",
})
app.get("/", (req, res)=>{
db.query("SELECT * FROM names", (err, result)=>{
res.render("home", {name: result[0].name})
})
})
app.listen("1999")
和hbs文件:
<html>
<head>
</head>
<body>
<b> {{name}} </b>
</body>
</html>
但是
但是这个项目只共享 mysql 结果的第一行 (jack)...
如何共享所有 mysql 结果?
https://handlebarsjs.com/guide/builtin-helpers.html#each
app.get("/", (req, res)=>{
db.query("SELECT * FROM names", (err, result)=>{
res.render("home", {names: result }) // pass all the results
});
});
hbs 文件:
使用 {{#each names }}
遍历所有名称
<html>
<head>
</head>
<body>
{{#each names }}
<h3> {{ this.name }} </h3>
{{/each}}
</body>
</html>
我用这个命令共享第一行 (jack)。 节点:
const express = require("express");
const app = express();
app.set("view engine","hbs")
const mysql = require("mysql")
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "stack",
})
app.get("/", (req, res)=>{
db.query("SELECT * FROM names", (err, result)=>{
res.render("home", {name: result[0].name})
})
})
app.listen("1999")
和hbs文件:
<html>
<head>
</head>
<body>
<b> {{name}} </b>
</body>
</html>
但是 但是这个项目只共享 mysql 结果的第一行 (jack)... 如何共享所有 mysql 结果?
https://handlebarsjs.com/guide/builtin-helpers.html#each
app.get("/", (req, res)=>{
db.query("SELECT * FROM names", (err, result)=>{
res.render("home", {names: result }) // pass all the results
});
});
hbs 文件:
使用 {{#each names }}
<html>
<head>
</head>
<body>
{{#each names }}
<h3> {{ this.name }} </h3>
{{/each}}
</body>
</html>