Node.js变量在函数外部使用

时间:2016-07-03 01:04:43

标签: javascript node.js express webstorm

我正在尝试这样做,以便我可以将我的trend变量从其函数传递到我的Pug模板的渲染器,我似乎无法做到。

var express = require('express');                                                                                                                                   
var router = express.Router();                                                                                                                                      
var googleTrends = require('google-trends-api');                                                                                                                    
var auth = require('http-auth');                                                                                                                                    
var ustrends;                                                                                                                                                       
var uktrends;                                                                                                                                                       
const Console = require('console').Console;                                                                                                                         
var basic = auth.basic({                                                                                                                                            
    realm: "Web."                                                                                                                                               
}, function (username, password, callback) { // Custom authentication method.                                                                                   
    callback(username === "user" && password === "pass");                                                                                                       
   }                                                                                                                                                               
);                                                                                                                                                                  

var find = ',';                                                                                                                                                     
var regex = new RegExp(find, 'g');                                                                                                                                  


googleTrends.hotTrends('US').then(function(trends){                                                                                                                 
ustrends = trends                                                                                                                                               
});                                                                                                                                                                 

googleTrends.hotTrends('EU').then(function(trends1) {                                                                                                               
uktrends = trends1                                                                                                                                              
});                                                                                                                                                                 

console.log(ustrends);                                                                                                                                              
/* GET home page. */                                                                                                                                                


router.get('/', auth.connect(basic), function(req, res, next) {                                                                                                     
res.render('index', {trends: ustrends.toString().replace(regex, ", "), trends1: uktrends.toString().replace(regex, ", "), title: 'Trends in the U.S & U.K'});   

});                                                                                                                                                                 

module.exports = router;                                                                                                                                                                

正如您所看到的,我正在尝试将“ustrends”和“uktrends”变量传递给渲染器。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

请记住using System; using System.Timers; public class Example { private static System.Timers.Timer aTimer; public static void Main() { SetTimer(); Console.WriteLine("\nPress the Enter key to exit the application...\n"); Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now); Console.ReadLine(); aTimer.Stop(); aTimer.Dispose(); Console.WriteLine("Terminating the application..."); } private static void SetTimer() { // Create a timer with a two second interval. aTimer = new System.Timers.Timer(2000); // Hook up the Elapsed event for the timer. aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true; } private static void OnTimedEvent(Object source, ElapsedEventArgs e) { Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", e.SignalTime); } } // The example displays output like the following: // Press the Enter key to exit the application... // // The application started at 09:40:29.068 // The Elapsed event was raised at 09:40:31.084 // The Elapsed event was raised at 09:40:33.100 // The Elapsed event was raised at 09:40:35.100 // The Elapsed event was raised at 09:40:37.116 // The Elapsed event was raised at 09:40:39.116 // The Elapsed event was raised at 09:40:41.117 // The Elapsed event was raised at 09:40:43.132 // The Elapsed event was raised at 09:40:45.133 // The Elapsed event was raised at 09:40:47.148 // // Terminating the application... will return a promise,因为它会从Google的API获得结果。由于渲染器位于其中hotTrendsustrends设置为值的回调之外,因此无法保证在调用渲染器之前设置这些值。

你可以使用几个嵌套的回调,但这会导致一些代码被推到相当远的位置;我推荐async库,它有一个名为series的函数,它允许你传入1)要按顺序执行的函数数组,以及2)在函数有执行后执行的回调。已完成,如果有一个错误,并且函数的结果作为参数,则会收到错误。在下面的代码段中,trend API会在调用渲染器之前返回结果:

uktrends