2

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. However, I am having trouble passing the single Object back to my routes.js file to route it to the edit page. 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:

<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. 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. Any tips or help would be appreciated, thanks! :)

6
  • Are you trying to create unique links to each agreement? (Which contain a little extra data like the roommates' names for context?) Commented Mar 29, 2018 at 1:44
  • Blake, in order to access the Agreements variable in the template, you need to first pass it in from the render call, just like how you passed in Employee. Commented Mar 29, 2018 at 1:52
  • Please post your get function in client-side Commented Mar 29, 2018 at 1:54
  • @StephenGheysens Once a specific agreement is clicked on, I want it to redirect to a page where the user can edit it. I know I will need to pass it from the render call, but I'm still working on getting the data there. That console.log function currently prints undefined in the console, which it wouldn't if the data was being passed correctly. Commented Mar 29, 2018 at 1:56
  • @ShimonBrandsdorfer I'm not quite sure what you mean. Can you clarify? Commented Mar 29, 2018 at 1:57

2 Answers 2

5

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 :

<% 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)
})
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer! This sounds promising, but I think I am still missing something. Here is what I have for my get route now: app.get('/editAgreement/:agreementID', isLoggedIn, function(req, res) { let agreementID = req.params.agreementID; var query = ("SELECT * FROM RoommateAgreement WHERE agreementID = ?"); connection.query(query, agreementID, function(err, rows, fields){ res.render('editAgreement.ejs', { Employee: req.user, Agreement: rows }); }); });
And here is what I have in EJS: <% for (var i=0; i < Agreements.length; i++) { %> <a href="/editAgreement/<% Agreements[i].agreementID %>" style= Unfortunately, I am getting a Cannot GET /editAgreement error. If these comments are confusing, let me know as the code looks jumbled up. Thanks!
you're welcome :) , your code looks ok, just decide whether to use var or let ( google that :P ) , i think you just need to restart the node server and refresh the page with ctrl+F5 , inspect the link to make sure you have /editAgreement/5 or some id in your html
I don't think the EJS file is building the link correctly. It's not adding the ID to the end.
oh, sorry, my bad, it should be <%= Agreement... %> , i forgot the =
|
0

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.

The quickest solution would be to use the querystring module. By requirting it on the top of your code, and then use it to send the agreement Object, as follows:

<% 

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.