Thursday, 27 April 2017

How do I get data from server route to javascript? Nodejs Express pug

I am pretty new at this so I hope i'm asking the right question, and approaching this the right way. I am trying to get data from the server. I have successfully passed it through a route to a pug page, but the javascript on that page does not have access to these objects. Here is the server code. Also posted is the client side javascript where I don't have access to the objects passed to the pug page.

router.get('/stockView', ensureAuthenticated, function(req, res, next){

  var s = req.user.stocks;
  var t = [];

  // get array of stock symbols
  for(var stock in s)
    {
      if (isNaN(parseInt(stock)))
      {    
      }
      else
      {
        s.push(req.user.stocks[stock].symbol);
      }
    }

  //pull historic data from yahoo-finance
  yahooFinance.historical({
    symbols: ['AAPL','YHOO'],
    from: '2012-01-01',
    to: '2012-02-28',
    period: 'd'  // 'd' (daily), 'w' (weekly), 'm' (monthly), 'v' (dividends only) 
  }, function (err, quotes) {

    if(err){
      // do nothing
    }else{
      res.render('stockView', {title: 'Stock View', user: req.user, stocks: quotes, mystocks: s, tickers: t });
    }

  });

});


// *********javascript code in stockView.pug file****************
script(type='text/javascript').
      $(function(){
        var tickers = #{title};



via Wesley Smith

No comments:

Post a Comment