Skip to content Skip to sidebar Skip to footer

Pass Variable From Express To Client JavaScript

Ultimately I'm trying to pass JSON data from the Node server to be used by D3 in the client. Here's my index.js var express = require('express'); var router = express.Router(); v

Solution 1:

You can't call the EJS variables from the clientside, you have to output the JSON to the page, and then fetch it

<script type="text/javascript">

   var json_data = <%- JSON.stringify( holdings ); %>

   // use the above variable in D3

</script>

<div class="portfolio">

    <h2><%= holdings.ReportHeading1 %></h2>
    <ul>
        <li>Cash: <%= holdings.CashMV %></li>
        <li>Fixed Income: <%= holdings.FixedMV %></li>
        <li>Equity: <%= holdings.EquityMV %></li>
        <li>Alternatives: <%= holdings.AltMV %></li>
    </ul>

    <div class="chart">
    </div>
</div> 

Post a Comment for "Pass Variable From Express To Client JavaScript"