I'm facing issue when passing a value from my express app.js file to ejs page.
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");
})
And here is my ejs file
<body>
<form 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>
When I run the server. it given an error in ejs file.
20|
<%=rp4you%>
21| <table>
22| <tr>
23| <th>Label</th>
rp4you is not defined at eval (eval at compile (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:662:12), :12:25) at index (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 [as engine] (C:\Users\sherg\desktop\price_calculator\node_modules\ejs\lib\ejs.js:489:10) at View.render (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\application.js:640:10) at Function.render (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\application.js:592:3) at ServerResponse.render (C:\Users\sherg\desktop\price_calculator\node_modules\express\lib\response.js:1012:7) at 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)
CodePudding user response:
Its because you are making a GET
request which is handled by this
app.get("/", function(req, res) {
res.render("index")
});
and not this
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,
});
});
Which means it doesn't have access to any of the variables.