将节点js变量传递给ejs模板

时间:2018-06-06 03:52:37

标签: javascript node.js ejs

我在节点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));        
        });

在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 %>

我想要的是将“projectedWallet”的值放在ejs文件中的textarea的post请求中,但我不知道该怎么做。

3 个答案:

答案 0 :(得分:1)

您需要通过EJS将projectedWallet送入textarea,如下所示:

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

...并在模板内的textarea中使用它

<强> 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>

答案 1 :(得分:0)

您可以将变量保存在响应的局部变量中,然后在前端使用它(在这种情况下为EJ)。

更新代码::

<强> 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>

答案 2 :(得分:0)

您需要检查是否定义了变量( projectedWallet ),在[CCode (cname = "uiWindow")] public class Window { [CCode (has_typedef = false, simple_generics = true)] public delegate int Callback<T> (T data); [CCode (cname = "uiWindowOnClosing", simple_generics = true)] public void on_closing<K> (Callback callback, K data); } 请求中,您没有将 projectedWallet 作为参数发送,因此为什么会出现错误

.get

如果有时使用变量多次渲染视图,而有时不使用变量,则需要添加条件并检查每个渲染是否存在该变量