简体   繁体   English

如何从服务器路由到javascript获取数据? Nodejs Express哈巴狗

[英]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. 我已成功通过路由将其传递给哈巴狗页面,但该页面上的javascript无法访问这些对象。 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. 还发布了客户端javascript,其中我无权访问传递给pug页面的对象。

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};

It doesn't work like that, Pug allows you to only handle the view and hence it directly renders the html view there. 它不是那样工作的,Pug只允许您处理视图,因此它直接在此处呈现html视图。 Pug Can only render views (HTML), It won't be able to give the javascript there access to the variables available to pug. Pug只能呈现视图(HTML),它将无法使javascript在那里访问可用于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. 您可以改用Ajax或Socket.io,以便将客户端的javascript代码与您在服务器端编写的javascript代码连接起来。

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. 这就是为什么我们拥有诸如socket.io和ajax之类的技术的原因,它们允许客户端与后端以及与我更喜欢使用Socket.io的Node联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将JSON数据从express传递到pug中的javascript / jQuery脚本? - How to pass JSON data from express to a javascript/jQuery script in pug? Pug-在Pug脚本中从服务器访问数据(type =&#39;text / javascript&#39;) - Pug - Access data from server in pug script(type='text/javascript') 如何将请求委托给 nodejs express 中的特定路由? - How do I delegate a request to a particular route in nodejs express? 如何从单行表中获取数据并在 javascript 中显示为哈巴狗 - How to get data from table in single row and show as pug in javascript 我如何从服务器获取JSON数据到javascript变量中 - How do I get JSON data from a server into a javascript variable 我不知道如何在 nodejs express 服务器中存储来自 api 调用的 json 数据,并将其发送到反应本机客户端 - I do not know how to store json data from an api call within a nodejs express server, and send it to a react native client "<i>How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template?<\/i>如何使用哈巴狗模板中的变量在身份验证(passport.js)成功和失败时呈现哈巴狗模板?<\/b> <i>(express, passport.js, pug)<\/i> (快递,passport.js,哈巴狗)<\/b>" - How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template? (express, passport.js, pug) 使用 nodejs 服务器和 Pug 如何一次显示一个列表中的元素并使用按钮进行迭代? - Using a nodejs server and Pug how can I display elements from a list one at a time and iterate using a button? 如何使用Express和MySQL将SQL查询的结果插入Pug模板中? - How do I insert results from an SQL query into a Pug template with Express and MySQL? nodejs express 无法从节点服务器获取 url 和数据 - nodejs express cannot get the url and data from node server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM