如何在express和parse中使用app.get中的app.locals
how to use app.locals inside app.get in express and parse
我正在使用express开发一个解析应用程序。我有一个索引文件,它向登录用户和公共用户显示相同的信息,除了登录到公共用户和注销到登录用户。我使用app.locals来存储标志和登录用户名,但有些是不存储的。我的app.js代码是
// The homepage renders differently depending on whether user is logged in.
app.get('/', function(req, res) {
if(Parse.User.current()){
Parse.User.current().fetch().then(function(user) {
// Render the user profile information (e.g. email, phone, etc).
name = user.get('username');
});
app.locals({flag :true,name:name});
}else{
app.locals({flag:false, name:''});
}
// Render a public welcome page, with a link to the '/' endpoint.
var ProfessionalUsers = Parse.Object.extend("User");
var query = new Parse.Query(ProfessionalUsers);
query.limit(50);
query.equalTo("usertype", "Professional");
query.find().then(function(profs) {
res.render('index', {
title: "Popular Experts",
profs: profs,
});
});
});
我在索引中写的代码
<div class='navigation'>
<ul class='navigation-ul'>
<li><a href='/' class='search' id='searchLink' >Search</a></li>
<% if(flag){ %>
<li><a href='/dashboard' class='dashboard' id='dashboardLink' >Dashboard</a></li>
<% } %>
<li><a href='/howitworks' class='how-it-works' id='howItWorksLink'>How it Works</a></li>
<li><a href='/testimonials' class='testimonials' id='testimonialsLink' >Testimonials</a></li>
<li><a href='/faqs' class='faqs' id='faqsLink'>FAQs</a></li>
<% if(flag){ %>
<li><a href='/logout' class='logout' id='logoutLink'>Logout<span class='username'>(<%= name ? name : '' %>)</span></a></li>
<% }else{ %>
<li><a href='/login' class='login-signup' id='navLoginSignupLink'>Login/Signup</a></li>
<% } %>
</ul>
</div>
向专业人士展示的代码
<p><%= title %></p>
<div class='professionals'>
<% for(var i=0;i<profs.length;i++) { %>
<div class='professional'>
<div class='picture-space' onclick = 'location.href = "/user/<%= profs[i].id %>"' style='cursor:pointer'>
<img src='images/default.jpg'/>
</div>
<div class='name-space'><%= profs[i].attributes.username %></div>
<div class='service-space'><%= profs[i].attributes.servicetype %></div>
<div class='linkedin-space'><a href='http://<%= profs[i].attributes.linkedin %>' target='_blank'>Linkedin Profile</a></div>
</div>
<% } %>
</div>
编辑:
这就是我为修复所做的
// The homepage renders differently depending on whether user is logged in.
app.get('/', function(req, res) {
// Render a public welcome page, with a link to the '/' endpoint.
var ProfessionalUsers = Parse.Object.extend("User");
var query = new Parse.Query(ProfessionalUsers);
query.limit(50);
query.equalTo("usertype", "Professional");
query.find().then(function(profs) {
var user = Parse.User.current();
if(user){
user.fetch().then(function(user,error) {
res.render('index',{user:user.toJSON(),profs:profs,title:'Popular Experts'});
});
}else{
res.render('index', {
title: "Popular Experts",
profs: profs,
user:''
});
}
});
});
app.locals无法访问req,res对象。它适用于每次请求都不会更改的非动态数据(如应用程序名称或类似内容)。我相信你想要的是res.locals(http://expressjs.com/api.html#res.locals)
我建议添加一些可以满足您需要的中间件。
你可以只为你的路线加载它。这将在中间件执行您在该路由上定义的功能之前执行它:
function getUserInfo(req, res, next) {
var user_name = "adam"; //make some call here to get the user data
//make 'user_name' variable available in the view
res.locals.user_name = user_name;
next(); //go to the next 'middleware', which is the stuff you defined.
}
//add getUserInfo here as middleware that will run on the '/' route BEFORE your middleware runs. Yours will be executed when 'next()' is called from the middleware function.
app.get('/', getUserInfo, function(req, res) {
相关文章:
- 等待 MongoDB findOne 回调完成,然后再完成 app.get()
- 表达快乐.js Restfy Koa app.listen() 和 app.get() 如何工作
- 如何避免为我的每个html文件添加ExpressJS app.get()
- 如何在express和parse中使用app.get中的app.locals
- 什么's将数组传递给app.get()的第一个参数的替换项
- 在连接DB - NodeJs的函数中使用app.get
- 如何将所选复选框的值发送到 app.get 路由
- NodeJS : app.get() 不像我所说的那样运行
- 为什么我不能从节点和 express.js 中的 app.get 函数中提取回调
- app.get and app.post get value in node.js
- Express app.get() equivalent in CasperJS
- Node.js Express app.get:浏览器想要打开链接
- Express Middleware为所有app.get()填充Jade变量
- 为什么总是触发“app.get('*')”
- 带有查询参数的节点js-app.get方法-内部服务器错误
- app.get在Node/Express中返回undefined
- app.get 不适用于基本路由
- Node.js Express app.get()没有'不在cron中工作
- 使用app.Get(节点)获取并渲染一些文件
- express.js:如何在app.get中使用http.request返回的值