简体   繁体   English

使用发布请求将nodejs变量发送到ejs模板

[英]Send nodejs variable to ejs template with post request

I use Express JS in my nodejs application to create a post request. 我在nodejs应用程序中使用Express JS创建发布请求。

IN INDEX.JS FILE I HAVE 在我拥有的INDEX.JS文件中

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

            res.locals.projectedWallet =  projectedWallet
            res.render('profile.ejs', {
                user : req.user,
            });
            console.log(JSON.stringify(projectedWallet));        
        });

And in profile.ejs I have 在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">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="/httpServer/index.js" />
    <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>

    </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" style="margin: 0px; width: 522px; height: 111px;">
                        <%=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>


            </div>
        </div>

    </div>

</div>
    <%- include footer %>

What I want is that, when I click the button in profile.ejs , the post request will work and do some function for me. 我想要的是,当我单击profile.ejs的按钮时,发帖请求将起作用并为我执行一些功能。 After that, the result of function will be stored in projectedWallet variable. 之后,函数的结果将存储在projectedWallet变量中。 When I pass this projectedWallet to ejs file - in this case profile.ejs - it will show up in value of textarea. 当我将这个projectedWallet传递给ejs文件(在本例中为profile.ejs )时,它将显示为textarea的值。 So, I want to make this variable to be accessed everywhere so I set 所以,我想让这个变量可以在任何地方访问,所以我设置了

res.locals.projectedWallet =  projectedWallet

But even with this, it still does not work. 但是即使如此,它仍然无法正常工作。 When I click button in profile.ejs, it return an error projectedWallet is not defined. 当我单击profile.ejs中的按钮时,它返回错误projectedWallet尚未定义。 I really don't know what to do next?! 我真的不知道下一步该怎么办?!

Do I have to include this node js file ( index.js ) into ejs template in head tag? 我是否必须将这个节点js文件( index.js )包含在head标签的ejs模板中?

Version of Express JS that i use is version 4. 我使用的Express JS版本是版本4。

Pass the projectedWallet variable in res.render res.render传递projectedWallet变量

 res.render('profile.ejs', { 
     user : req.user, 
     projectedWallet: projectedWallet });
  1. You can the projectedWallet variable in res.render 您可以在res.render中使用projectedWallet变量

     res.render('index', {projectedWallet:projectedWallet}, function(err, html) { console.log(html); res.send('done'); }) 
    1. Second option will be to use session and accessing the sesssion variable 第二种选择是使用会话并访问sesssion变量

      var session = require('express-session'); var session = require('express-session');

      req.session.projectedWallet=projectedWallet; req.session.projectedWallet = projectedWallet;

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

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