Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将节点js变量传递给ejs模板_Javascript_Node.js_Ejs - Fatal编程技术网

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

Javascript 将节点js变量传递给ejs模板,javascript,node.js,ejs,Javascript,Node.js,Ejs,我在节点js文件“index.js”中有一个post请求 在profile.ejs中显示给客户看,我有:这是完整的代码,我只是按照要求编辑 <html lang="en"> <head> <title>Node Authentication</title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.m

我在节点js文件“index.js”中有一个post请求

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

节点身份验证
正文{padding top:80px;换行符:break word;}

个人资料页 测试 地方的 id
用户名
密码

钱包

我想把“
projectedWallet
”的值放在ejs文件的文本区域的post请求中,但我不知道怎么做。

您将希望通过ejs将
projectedWallet
输入文本区域,如下所示:

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));        
        });
<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>
。。。并在模板的文本区域中使用它

profile.ejs

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


id
用户名
密码

钱包
您可以将变量保存在响应的局部变量中,然后在前端使用它(本例中为EJs)

更新代码::

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));        
        });
<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>
profile.ejs

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


id
用户名
密码

钱包
您需要检查变量(projectedWallet)是否已在
获取请求中定义。您没有将projectedWallet作为参数发送,因此会出现错误

<% if(typeof projectedWallet !== 'undefined') { %>
   <p><%= projectedWallet %></p>
<% } %>


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

它不起作用!出现错误:39 | 40 | 41 | projectedWallet未启动defined@Meowth2018我注意到在index.js文件中有
res.render('profile.ejs',…)
。您正在渲染profile.ejs,而不是index.ejs。你在哪里渲染index.ejs?对不起,我打错了。我使用的文件是profile.ejs,不是index.ejs,但这不是问题,问题是渲染后projectedWallet没有在profile.ejs中定义。您确定始终定义了
projectedWallet
?还有,我可以问一下您使用的是什么版本的EJS吗?它仍然返回一个错误“projectedWallet未定义”请帮助我!请分享你的整个index.js文件。请看我的问题。我只是按照你的要求编辑的。请帮帮我!有些地方说我必须声明“中间件功能”,但我真的不知道从哪里开始,也不知道怎么做。请帮帮我!!!