简体   繁体   English

如何将对象从EJS传递给Node.js路由器 - Javascript,EJS,Node.js,MySQL

[英]How to pass an object from EJS to Node.js Router - Javascript, EJS, Node.js, MySQL

I am working on looping through records in a MySQL table, displaying them in an EJS file, and when one is clicked on, routing to a page to edit the record. 我正在处理MySQL表中的记录循环,将它们显示在EJS文件中,当单击一个时,路由到页面以编辑记录。 However, I am having trouble passing the single Object back to my routes.js file to route it to the edit page. 但是,我无法将单个Object传递回我的routes.js文件以将其路由到编辑页面。 I have had no problem looping through all the records and printing them, so I know the data is there. 我没有问题循环所有记录并打印它们,所以我知道数据存在。 Here is the part of the EJS file where I am outputting all of the records for user selection: 这是EJS文件的一部分,我输出所有用户选择的记录:

<body>
<div class="container">

<div class="col-sm-6 col-sm-offset-3">

    <h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
    <p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>

    <% for (var i=0; i < Agreements.length; i++) { %>
        <a href="/editAgreement" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
                <p><%= Agreements[i].roommate1%></p>
                <p><%= Agreements[i].roommate2%></p>
                <% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
            </div>
        </a>
    <% } %>

    <hr>
    <p align="center">
        <a href="/agreement" class="btn btn-default btn-sm"><span class="fa fa-arrow-circle-left"></span> Back</a>
        <a href="/logout" class="btn btn-default btn-sm"><span class="fa fa-sign-out-alt"></span> Logout</a>
    </p>

</div>

</div>
</body>

And here is the Get function to route the object to the edit page. 这里是Get函数将对象路由到编辑页面。 You can see where I'm trying to log the object to the console to make sure I'm getting the right information, but it's currently printing as "undefined". 您可以看到我在哪里尝试将对象记录到控制台以确保我获得正确的信息,但它当前打印为“未定义”。

app.get('/editAgreement', isLoggedIn, function(req, res) {
    res.render('editAgreement.ejs', {
        Employee: req.user
    })
    console.log(req.body.Agreement);
})

I'm not sure if the problem is how I'm declaring the object variable in EJS or how I'm trying to use it in the Get function, as I am new to Javascript. 我不确定问题是我是如何在EJS中声明对象变量的,或者我是如何在Get函数中使用它的,因为我是Javascript的新手。 Any tips or help would be appreciated, thanks! 任何提示或帮助将不胜感激,谢谢! :) :)

to passe variables to another page using GET you have to passe them in the url , with that you will expose you data and that's not safe and a lot of work if you have a lot of information to pass,you should passe only the id of the agreement to the get route, fetch the agreement and then render the view, like : 要使用GET将变量传递到另一个页面,你必须将它们传递到url ,这样你就会暴露你的数据,如果你有大量的信息需要通过,那就不安全了很多工作,你应该只传递id get路由的协议,获取协议然后呈现视图,如:

<% for (var i=0; i < Agreements.length; i++) { %>
    <a href="/editAgreement/<%= Agreements[i].id %>" style="text ...

and then : 接着 :

app.get('/editAgreement/:id', isLoggedIn, function(req, res) {
    let agreementId = req.params.id;

    let agreement = // go fetch the agreement from the database like you did before with the list of agreements

    res.render('editAgreement.ejs', agreement)
})

If you want to get the Object sent along with your GET request, you need to send them along, and it doesn't seem from your code that you are doing it. 如果您希望将对象与GET请求一起发送,则需要将它们一起发送,并且您的代码中似乎没有这样做。

The quickest solution would be to use the querystring module. 最快的解决方案是使用querystring模块。 By requirting it on the top of your code, and then use it to send the agreement Object, as follows: 通过在代码顶部重新编写它,然后使用它发送协议Object,如下所示:

<% 

 const querystring = require('querystring');
 for (var i=0; i < Agreements.length; i++) { 
       var Agreement = Agreements[i]
 %>
        <a href="/editAgreement/?<%= querystring.stringify(Agreement) %>" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreement.roomNumber%></h3>
                <p><%= Agreement.roommate1%></p>
                <p><%= Agreement.roommate2%></p>
            </div>
        </a>
    <% } %>

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

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