简体   繁体   English

将Node JS变量传递给EJS模板

[英]Pass node js variable to ejs template

I have a post request in node js file "index.js" 我在节点js文件“ 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. 在profile.ejs中显示给客户看,我有:这是我按要求进行编辑的完整代码。

<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. 我想要的是在post请求中将“ projectedWallet ”的值放在ejs文件的textarea中,但是我不知道该怎么做。

You will want to feed projectedWallet into the textarea through EJS like so: 您将希望通过EJS将projectedWallet馈送到textarea中,如下所示:

index.js 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 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>

you can save the variable in response's local variables and then use it at frontend side (EJs in this case). 您可以将变量保存在响应的局部变量中,然后在前端使用它(在这种情况下为EJ)。

updated code:: 更新的代码::

index.js 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 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>

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 您需要检查是否定义了变量( projectedWallet ),在.get请求中您没有将projectedWallet作为参数发送,因此这就是为什么出现错误的原因

<% 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 如果多次渲染视图有时有时使用变量,而其他时候不使用变量,则需要添加条件并检查每个渲染是否存在该变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM