express - 将值传递给 EJS
问题描述
将值从我的 express app.js 文件传递到 ejs 页面时,我遇到了问题。
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render("index")
});
app.post("/", (req, res) => {
const value = req.body.value;
function RoundTo(number, roundto){
return roundto * Math.round(number/roundto);
}
const a = RoundTo(((((value / 10) + 2) + (value * 1.1)) * 1.05), .25);
const b = RoundTo(((((value / 10) + 2.3) + (value * 1.4)) * 1.05), .25);
const c = ((value * 1.12) + 0.5).toFixed(2);
const d = ((value * 1.17) + 1).toFixed(2);
const e = (((value * 1.15) * 1.05) + .7).toFixed(2);
const f = RoundTo(((((value / 10) + 4) + (value * 1.1)) * 1.2), 0.25);
const g = RoundTo(((((value / 10) + 4) + (value * 1.2)) * 1.5), 0.50);
res.render("index", {
rp4you: a,
cp4you: b,
b2b4youa: c,
b2b4youb: d,
valueptsb: e,
rptsb: f,
cptsb: g,
});
});
app.listen(3000, function() {
console.log("Server is Up and Running on Port 3000");
})
这是我的 ejs 文件
<body>
<form class="" action="/" method="post">
<input type="number" step="any" name="value" value="">
<button type="submit" name="submit">Submit</button>
</form>
<br>
<hr>
<br>
<p><%=rp4you%></p>
<p><%=cp4you%></p>
<p><%=b2b4youa%></p>
<p><%=b2b4youb%></p>
<p><%=costtsb%></p>
<p><%=rptsb%></p>
<p><%=cptsb%></p>
</body>
当我运行服务器时。它在 ejs 文件中给出了一个错误。
20|
<%=rp4you%>
21| <table>
22| <tr>
23| <th>Label</th>
rp4you 未在 eval 处定义(编译时的 eval (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:662:12), :12:25) 在索引 (C:\Users\ sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:692:17) at tryHandleCache (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:272:36) at View .exports.renderFile [作为引擎] (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:489:10) 在 View.render (C:\Users\sherg\desktop\price_calculator\ node_modules\express\lib\view.js:135:8) 在 tryRender (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\application.js:640:10) 在 Function.render (C:\ Users\sherg\desktop\price_calculator\node_modules\express\lib\application.js:592:3) 在 ServerResponse.render (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\response.js:1012: 7) 在 C 处:\Users\sherg\desktop\price_calculator\app.js:15:7 at Layer.handle [as handle_request] (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\router\layer.js:95: 5)
解决方案
这是因为您正在发出GET
由 this 处理的请求
app.get("/", function(req, res) {
res.render("index")
});
而不是这个
app.post("/", (req, res) => {
const value = req.body.value;
function RoundTo(number, roundto){
return roundto * Math.round(number/roundto);
}
const a = RoundTo(((((value / 10) + 2) + (value * 1.1)) * 1.05), .25);
const b = RoundTo(((((value / 10) + 2.3) + (value * 1.4)) * 1.05), .25);
const c = ((value * 1.12) + 0.5).toFixed(2);
const d = ((value * 1.17) + 1).toFixed(2);
const e = (((value * 1.15) * 1.05) + .7).toFixed(2);
const f = RoundTo(((((value / 10) + 4) + (value * 1.1)) * 1.2), 0.25);
const g = RoundTo(((((value / 10) + 4) + (value * 1.2)) * 1.5), 0.50);
res.render("index", {
rp4you: a,
cp4you: b,
b2b4youa: c,
b2b4youb: d,
valueptsb: e,
rptsb: f,
cptsb: g,
});
});
这意味着它无权访问任何变量。
推荐阅读
- php - 为什么 require(local_file) 或 include(local_file) 可能会失败以及如何处理?
- c# - 如何在 odata v4 代码生成器中进行动态查询
- javascript - 尝试映射一组 url 图像以使用 map 渲染到卡片
- laravel - 单击提交按钮后,Laravel 表单无法正常工作
- security - 使用 JWT 生成 API 密钥并在需要时重新生成相同的密钥
- hsqldb - 如何创建持久化 HSQL 数据库?
- sql - 如何在 Oracle SQL 中将整数除以产生浮点数?
- matlab - 是否有一个内置函数可以检查变量是否是 Matlab 中最小长度的单元格?
- google-cloud-platform - Google Composer Dag 不会自动触发,甚至不会通过 CMD 触发
- java - 在Java 8中将对象列表分组并将子组减少为不同的对象