Reputation: 31
I have a post request in node js file "index.js"
this.app.post('/profile', (req, res) => {
let password = req.body.password;
let newWallet = operator.createWalletFromPassword(password);
let projectedWallet = projectWallet(newWallet);
res.render('profile.ejs', {
user : req.user,
});
console.log(JSON.stringify(projectedWallet));
});
And in profile.ejs to show for client see, I have: THIS IS FULL CODE THAT I JUST EDIT AS REQUESTED.
<html lang="en">
<head>
<title>Node Authentication</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
<style>
body { padding-top:80px; word-wrap:break-word; }
</style>
</head>
<br>
<%- include header %>
<div class="container">
<div class="page-header text-center">
<h1><span class="fa fa-anchor"></span> Profile Page</h1>
<a href="/logout" class="btn btn-default btn-sm">Logout</a>
<form action="/profile" method="get">
<button type="submit" class="btn btn-warning btn-lg" id="sub" >test</button>
</form>
</div>
<div class="row">
<!-- LOCAL INFORMATION -->
<div class="col-sm-6">
<div class="well">
<h3><span class="fa fa-user"></span> Local</h3>
<form action="/profile" method="post">
<p>
<strong>id</strong>: <%= user.id %><br>
<strong>username</strong>: <%= user.username %><br>
<strong>password</strong>: <%= user.password %>
</p>
<textarea id="myTextArea" cols=50 rows=10><%= data %>
</textarea>
<!-- these fields will be sent to server -->
<input type="hidden" name="username" value="<%= user.username %>">
<input type="hidden" name="password" value="<%= user.password %>">
<button type="submit" class="btn btn-warning btn-lg" id="sub" >Wallet</button>
</form>
</div>
</div>
</div>
</div>
<%- include footer %>
What I want is to put value of "projectedWallet
" in post request to textarea in ejs file, but I don't know how to do that.
Upvotes: 1
Views: 6192
Reputation: 3177
You need to check if the variable (projectedWallet) is defined, in the .get
request you are not sending the projectedWallet as a parameter, so that's why you are getting the error
<% if(typeof projectedWallet !== 'undefined') { %>
<p><%= projectedWallet %></p>
<% } %>
If you render a view multiple times sometimes with variables and others without, you need to add a condition and check if that variable exist for each render
Upvotes: 0
Reputation: 36
you can save the variable in response's local variables and then use it at frontend side (EJs in this case).
updated code::
index.js
this.app.post('/profile', (req, res) => {
let password = req.body.password;
let newWallet = operator.createWalletFromPassword(password);
let projectedWallet = projectWallet(newWallet);
//added line
res.locals.projectedWallet = projectedWallet
res.render('profile.ejs', {
user : req.user,
});
console.log(JSON.stringify(projectedWallet));
});
profile.ejs
<form action="/profile" method="post">
<p>
<strong>id</strong>: <%= user.id %><br>
<strong>username</strong>: <%= user.username %><br>
<strong>password</strong>: <%= user.password %>
</p>
<!-- updated line -->
<textarea id="myTextArea" cols=50 rows=10><%=projectedWallet %>
</textarea>
<!-- these fields will be sent to server -->
<input type="hidden" name="username" value="<%= user.username %>">
<input type="hidden" name="password" value="<%= user.password %>">
<button type="submit" class="btn btn-warning btn-lg">Wallet</button>
</form>
Upvotes: 0
Reputation: 475
You will want to feed projectedWallet
into the textarea through EJS like so:
index.js
this.app.post('/profile', (req, res) => {
let password = req.body.password;
let newWallet = operator.createWalletFromPassword(password);
let projectedWallet = projectWallet(newWallet);
res.render('profile.ejs', {
user : req.user,
// We are now feeding your EJS template another variable
projectedWallet : JSON.stringify(projectedWallet),
});
console.log(JSON.stringify(projectedWallet));
});
... and use it within the textarea inside your template
profile.ejs
<form action="/profile" method="post">
<p>
<strong>id</strong>: <%= user.id %><br>
<strong>username</strong>: <%= user.username %><br>
<strong>password</strong>: <%= user.password %>
</p>
<textarea id="myTextArea" cols=50 rows=10>
<!-- We now populate the template with the sent variable -->
<%= projectedWallet %>
</textarea>
<!-- these fields will be sent to server -->
<input type="hidden" name="username" value="<%= user.username %>">
<input type="hidden" name="password" value="<%= user.password %>">
<button type="submit" class="btn btn-warning btn-lg">Wallet</button>
</form>
Upvotes: 2