I'm using my node app to retrieve some data from an API and want to display that data in a view and allow the user to change it there. I'm calling the api and get a JSON response which I'd like to pass onto the view. Here's what I'm doing:
var query = 'SELECT Id, OwnerId, ActivityDateTime, EndDateTime, WhatId, WhoId, Subject FROM Event WHERE OwnerId = \'' + user.userId + '\'' ;
// execute the query and get response
...
...
var ev = resp.records;
res.render('index.html', {eventData : ev});
Problem being that when I then want to use the data in my view, I get an undefined error.
If I do:
<div id='Subject'>#{eventData.Subject}</div>
I get a print-out of #{eventData.Subject}
Trying to use the data in JS returns an undefined error:
<script type="text/javascript">
var mydata = #{eventData};
</script>
After some searching I found a post that suggested to stringify it first:
var mydata = !{JSON.stringify(eventData)};
console.log(mydata);
Here, I get a syntax error "SyntaxError: missing : after property id"
I'm pretty sure I'm missing something basic and can't for the life of me figure it out. I've found a lot of articles examples that explain how to do it in Jade, but I don't want to use Jade and using the recommendations from the articles results in "undefined" or "illegal character" errors.
I'm all out of ideas... help? Tnx
Jonathan Ong's comment actually brought me onto the right track. Since I was using a customer handler for html, it wouldn't interpret any variables.
I switched to using ejs:
// view engine ejs
app.set('view engine', 'ejs');
app.register('.html', require('ejs'));
and used the following placeholder:
<%= eventData.Subject %>
This actually made my data show up. Thanks for all the comments and answers! Michael
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With