【发布时间】: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