Human Cyborg Relations
Human Cyborg Relations

Reputation: 1244

Passing data from one page to another using Node, Express & EJS

I've got the following routes set up:

router.get('/', function(req, res) {
    res.render('index', {});
});

router.post('/application', function(req, res) {
  res.render('application', {twitchLink : req.query.twitchLink});
});

I've got the two views set up properly.

This is what I've got in the 'index' view:

<form class="form-horizontal" action="/application", method="post", role="form">
    <input type="url" name="twitchLink" required>
    <button class="btn btn-success">Submit</button>
</form>

Submitting this form does take me to the application view.

<script>var twitchLink = <%- JSON.stringify(twitchLink) %></script>
<script>console.log(twitchLink)</script>

This should log out the link that I submitted, right? However, I get these two lines:

Uncaught SyntaxError: Unexpected end of input
Uncaught ReferenceError: twitchLink is not defined

Upvotes: 0

Views: 1320

Answers (1)

Daniel Yefet
Daniel Yefet

Reputation: 91

I think you need to put quotes around <%- JSON.stringify(twitchLink) %>, like this:

var twitchLink = '<%- JSON.stringify(twitchLink) %>'

In your example, it will come out as:

var twitchLink = foo.bar.com

What you want is:

var twitchLink = 'foo.bar.com'

Upvotes: 1

Related Questions