Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
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};
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It doesn't work like that, Pug allows you to only handle the view and hence it directly renders the html view there. Pug Can only render views (HTML), It won't be able to give the javascript there access to the variables available to pug.

You can use Ajax or Socket.io instead so your javascript code on the client side is connected with the javascript code you write on the server-side.

That's Why we've technologies like socket.io and ajax, they allow the client side to be in touch with the backend and with Node I prefer to use Socket.io.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda