Node.js数组无法呈现

时间:2017-12-02 22:34:46

标签: node.js express pug

我无法在jade视图中渲染js文件中的数组。我不知道为什么它不起作用 这就是我将数据添加到数组的方式。我知道调用有效,因为内部console.log显示正确的值。但是函数外部的那个返回一个未定义的引用。

UPDATE 我发现一旦函数结束,数组的填充方式就不存在了。

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

    var auth = "Basic " + new Buffer(username + ':' + password).toString('base64');

    if(name !=undefined){
      names.forEach(function(value){
        console.log(value);
        var request = https.request({
            method: "GET",
            host: "api.intrinio.com",
            path:"/news?ticker="+value+"&page_size=1",
            headers: {
                "Authorization": auth

            }
        }, function(response) {
            var json = "";
            response.on('data', function (chunk) {
                json += chunk;
            });
            response.on('end', function() {
                var company = JSON.parse(json);

                info.push(company.data[0].summary);
                urls.push(JSON.stringify(company.data[0].url));
                console.log("this is ithe value inner" + urls[0]);

            });
        });
        request.end();
    });
    console.log("this is ithe value" + urls[0]);
    if(info[0] != undefined){

      res.render('news', { title: "StockSim",stockInfo:info,stockUrls:urls,stockNames:names});
    }
  }
  else{
    res.redirect('back');
  }
 });

 module.exports = router;

news.jade

   doctype html
  html
   head
    title= title
    link(rel='stylesheet', href='/stylesheets/main.css')
body
    #leftNav
        h1 Stock News
        ul
            li
                a(href='') Home
            li
                a(href='/index') Browse
            li
                a(href='/news') news
            li
                a(href='') Settings
        #account
            img#accountImage(src='/images/missing.png')
            p= accountName
    #container
        each name in stockUrls
            a(href=name) Test

0 个答案:

没有答案