将EJS变量传递给app.post节点js

时间:2018-06-05 08:13:30

标签: javascript html node.js ejs

我的ejs文件包含如下变量:

<form action="/profile" method="post">
<p>
    <% var ten = user.username %>
    <strong>id</strong>: <%= user.id %><br>
    <strong>username</strong>: <%= ten %><br>
    <strong>password</strong>: <%= user.password %>
</p>
<p id="passwd" name="passwd" ><%= user.username %></p>
<button type="submit" class="btn btn-warning btn-lg">Wallet</button>
</form>

在index.js文件中,我有POST请求想要变量&#34; ten&#34;并像这样添加password变量。

this.app.post('/profile', (req, res) => {                        
            let password = req.body.ten;            
            let newWallet = operator.createWalletFromPassword(password);
            let projectedWallet = projectWallet(newWallet);
            res.status(201).send(projectedWallet)
        });

但它不起作用。我甚至尝试使用:

let password = req.body.passwd;  
let password = req.body[passwd];

但它仍然不起作用!

1 个答案:

答案 0 :(得分:0)

你需要使用输入,选择,textarea等表单元素。如果你不想让它可见,那么使用隐藏类型如下

<form action="/profile" method="post">
    <p>
        <% var ten = user.username %>
        <strong>id</strong>: <%= user.id %><br>
        <strong>username</strong>: <%= ten %><br>
        <strong>password</strong>: <%= user.password %>
    </p>
    <p id="passwd" name="passwd" ><%= user.username %></p>
    <!-- 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>