2016-08-04 16 views
0

试图了解node.js和express的工作原理。我已经能够从我的mongo数据库查询数据了...现在我正在玩弄试图了解如何在我的路由器代码和我的视图之间传递数据。node.js/express应用程序失败,但未定义变量的错误

我在locations.js文件这段代码在我的路线文件夹:

router.get('/', function(req, res, next) { 
    console.log('Inside GET method'); 
    loc.find(function (err, locations) { 
    if (err) return next(err); 
     // res.json(locations); THIS WORKS. 
     // res.render('index', {loc_data:res.json(locations)}); THIS FAILS 
      res.render('index', {loc_data:locations}); //FAILS 
    }); 
}); 

而这正是我的index.ejs文件看起来像:

<!DOCTYPE html> 
<html ng-app> 
    <head> 
    <title>test</title> 
    <link rel='stylesheet' href='/stylesheets/style.css' /> 
    </head> 
    <body> 
    <h1>location data</h1> 
    <p><%= loc_data %></p> 
    </body> 
</html> 

目前,代码失败的错误:loc_data未定义 我正在通过逆向工程学习其他人创建的演示应用程序。 所以我可能会错过一些非常基本的东西。

任何提示将不胜感激。

感谢。

回答

0

Doh!很简单。意识到在改变代码之后......我没有重新启动应用程序。我只是希望它自动刷新。所以,现在我正在更改代码...重新运行“node app.js”(之前未执行此步骤),然后刷新我的浏览器,这很好。