Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda