【问题标题】:node.js / express app failing with error that a variable hasn't been definednode.js / express 应用程序失败并出现未定义变量的错误
【发布时间】:2016-08-04 17:36:01
【问题描述】:

尝试了解 node.js 和 express 的工作原理。我已经能够从我的 mongo 数据库中查询数据......现在我正在尝试了解如何在我的路由器代码和我的视图之间传递数据。

我的 routes 文件夹中的 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 我正在通过逆向工程来学习其他人创建的演示应用程序。 所以我可能错过了一些非常基本的东西。

任何提示将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript node.js mongodb express


    【解决方案1】:

    哇!很简单。意识到在进行代码更改后......我没有重新启动应用程序。我只是希望它会自动刷新。所以现在我正在更改我的代码......重新运行“node app.js”(之前错过了这一步)然后刷新我的浏览器就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多