【问题标题】:Passing value to EJS将值传递给 EJS
【发布时间】:2021-10-29 21:20:20
【问题描述】:

将值从我的 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|

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) 在 tryHandleCache (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:272:36) 在 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 在 Layer.handle [as handle_request] (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\router\layer.js:95:5)

【问题讨论】:

    标签: express ejs


    【解决方案1】:

    这是因为您正在发出由 this 处理的 GET 请求

    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,
      });
    
    });
    

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

    【讨论】:

    • 嗨,你能澄清一下吗?我提出了渲染路线的获取请求。然后发布请求以发布变量。
    • @BuFahad 这不是它的工作原理。每个res.render 调用都需要一组传递给它的变量。当您发出 get 请求时,该请求由第一个处理程序处理,该处理程序简单地调用 res.render 而不传递任何其他变量。当您发出 post 请求时,第二个处理程序会拾取它并使用变量调用 res.render。它们不共享。
    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2016-03-04
    • 2016-10-06
    • 2022-01-05
    • 2014-06-30
    • 2014-01-30
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多