Passing data to view in node + express

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.

  • Call function inside mongodb's aggregate?
  • Passport-local returns 400 error, never queries database
  • Node JS - read file properties
  • Mongoose Schema.update doesn't update boolean
  • How to know if node-webkit app is running with Administrator/elevated privilege?
  • how to change skipper maxTimeToBuffer attribute
  • 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

  • Webpack Aliases in Node JS Server code
  • Repository like pattern with Node and mongoose
  • How to host 3 node apps with 3 different domains on one VPS?
  • How Kill node process on killing forever process
  • DB connection error handling with monk
  • Cassandra nodejs DataStax driver don't return newly added columns via prepared statement execution
  • One Solution collect form web for “Passing data to view in node + express”

    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

    Node.js is the Best Javascript runtime in the world.