ส่งตัวแปร nodejs ไปยังเทมเพลต ejs พร้อมคำขอโพสต์

ฉันใช้ Express JS ในแอปพลิเคชัน nodejs ของฉันเพื่อสร้างคำขอโพสต์

ในไฟล์ 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));        
        });

และในprofile.ejs ฉันมี

<html lang="en">
<head>
    <title>Node Authentication</title>
    <link rel="stylesheet" href="/th//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="/th//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="/th/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 %>

สิ่งที่ฉันต้องการคือเมื่อฉันคลิกปุ่มใน profile.ejs คำขอโพสต์จะทำงานและทำหน้าที่บางอย่างให้ฉัน หลังจากนั้นผลลัพธ์ของฟังก์ชันจะถูกเก็บไว้ในตัวแปร projectedWallet เมื่อฉันส่ง projectedWallet นี้ไปยังไฟล์ ejs - ในกรณีนี้ profile.ejs - มันจะแสดงเป็นค่าของ textarea ดังนั้นฉันจึงต้องการให้ตัวแปรนี้เข้าถึงได้ทุกที่ดังนั้นฉันจึงตั้งค่า

res.locals.projectedWallet =  projectedWallet

แต่ถึงอย่างนั้นก็ยังใช้งานไม่ได้ เมื่อฉันคลิกปุ่มใน profile.ejs มันส่งคืนข้อผิดพลาด projectedWallet ไม่ได้กำหนดไว้ ฉันไม่รู้จริงๆว่าจะทำยังไงต่อไป!

ฉันต้องรวมไฟล์ node js นี้ (index.js) ลงในเทมเพลต ejs ในแท็ก head หรือไม่

เวอร์ชันของ Express JS ที่ฉันใช้คือเวอร์ชัน 4


person Hoai An    schedule 07.06.2018    source แหล่งที่มา
comment
เพิ่ม projectedWallet ใน Index.js res.render('profile.ejs', { user : req.user, projectedWallet });   -  person Shubham Batra    schedule 07.06.2018
comment
มันยังคงส่งคืน projectedWallet ไม่ได้ถูกกำหนดไว้ในprofile.ejs   -  person Hoai An    schedule 07.06.2018


คำตอบ (2)


ส่งผ่านตัวแปร projectedWallet ใน res.render

 res.render('profile.ejs', { 
     user : req.user, 
     projectedWallet: projectedWallet });
person mtshaikh    schedule 07.06.2018
comment
มันยังคงส่งคืน projectedWallet ไม่ได้ถูกกำหนดไว้ในprofile.ejs ฉันทำทุกอย่างแล้ว แต่ยังคงส่งคืนข้อผิดพลาด - person Hoai An; 07.06.2018
comment
ข้อผิดพลาดนี้เกิดขึ้นเมื่อคุณโหลดprofile.ejsครั้งแรกหรือหลังจากที่คุณส่งคำขอโพสต์หรือไม่ - person mtshaikh; 07.06.2018

  1. คุณสามารถตัวแปร projectedWallet ใน res.render

    res.render('index', {projectedWallet:projectedWallet}, function(err, html) {
            console.log(html);
            res.send('done');
        })
    
    1. ตัวเลือกที่สองคือการใช้เซสชันและการเข้าถึงตัวแปรเซสชัน

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

      req.session.projectedWallet=projectedWallet;

person Rubin bhandari    schedule 07.06.2018
comment
หมายความว่ายังไง นี่คือ 2 ตัวเลือกแยกกันเหรอ? ขวา? - person Hoai An; 07.06.2018
comment
ถึงอย่างนั้นก็ไม่มีตัวเลือกใดเลยที่ส่งคืนผลลัพธ์ความสำเร็จ - person Hoai An; 07.06.2018