我刚刚开始使用webdev和Express,并对流程感到困惑。
在我的app.js
:
// RESTful routes for the home object.
app.get('/home', homeController.index);
app.get('/home/languages/:id', homeController.languages);
在我的home.js
路线中:
var _ = require('underscore');
// Display all users.
exports.index = function(req, res) {
Parse.Cloud.run('getAllUsers', {}, {
success: function(result) {
console.log("result = " + result);
res.render('home/index', {
users: result
});
},
error: function(error) {
}
});
};
// Display all the available languages
exports.languages = function(req, res) {
Parse.Cloud.run('getAllLanguages', {}, {
success: function(result) {
res.render('home/languages', {
languages: result
});
},
error: function(error) {
}
});
};
最后,HTML我试图用Language对象显示下拉列表:
<div class="page-header" id="languages">
<div class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Choose Language</a>
<b class="caret"></b>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<% languages.forEach(function(language) { %>
<li><a href="<%= '/languages/' + language.id %>"><%= language.get("name") %></a></li>
<% }) %>
</ul>
</ul>
</div>
</div>
我能够获取用户并显示它们 - 我没有包含该部分的HTML。但是,我没有正确路由GET语言请求,因为我收到以下错误:
ReferenceError: cloud/views/home/index.ejs:17
15| <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
16|
>> 17| <% languages.forEach(function(language) { %>
18| <li><a href="<%= '/languages/' + language.id %>"><%= language.get("name") %></a></li>
19| <% }) %>
20|
languages is not defined
at eval (eval at <anonymous> (ejs.js:256:12), <anonymous>:33:448)
at ejs.js:261:15
at Object.exports.render (ejs.js:299:13)
at View.exports.renderFile [as engine] (ejs.js:325:22)
at View.render (express_view.js:77:8)
at Function.app.render (express_application.js:516:10)
at res.render (express_response.js:763:7)
at Parse.Cloud.run.success (controllers/home.js:10:10)
at Parse.js:2:4591
at r (Parse.js:2:3909)
修改
这是第二个版本,可以在languages
查询的回调中查询users
。
exports.index = function(req, res) {
console.log("users first");
Parse.Cloud.run('getAllUsers', {}, {
success: function(users) {
Parse.Cloud.run('getAllLanguages', {}, {
success: function(languages) {
res.render('home/index', {
languages: languages,
users: users
});
},
error: function(error) {
}
});
},
error: function(error) {
}
});
};