首页 > 解决方案 > 将值传递给 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)

标签: expressejs

解决方案


这是因为您正在发出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,
  });

});

这意味着它无权访问任何变量。


推荐阅读