n-d*_*evr 5 mongodb node.js express keystonejs
我有一个加载公寓列表并显示它们的路线:
app.get( '/condo-list', middleware.loadCondoList, routes.views.condolist );
Run Code Online (Sandbox Code Playgroud)
loadCondoList中间件调用CondoBuilding模型并在res.locals上设置结果:
exports.loadCondoList = function loadCondoList( req, res, next ) {
console.log( 'request url: ' + req.url );
console.log( 'getting condo buildings...' );
CondoBuilding.model
.find()
.exec( ( err, condos ) => {
if ( err ) {
// pass error along
next( err );
} else {
// add CondoBuildings to locals
res.locals.condoBuildings = condos;
next();
}
});
};
Run Code Online (Sandbox Code Playgroud)
对数据库的调用成功,页面按预期呈现.但是,由于某种原因,路线运行了两次.控制台输出如下:
request url: /condo-list
getting condo buildings...
GET /condo-list 304 344.532 ms
request url: /condo-list
getting condo buildings...
GET /condo-list 304 317.631 ms
Run Code Online (Sandbox Code Playgroud)
我已经在多个浏览器(Chrome,Safari,Firefox)中重现了这种行为,并确认在任何其他路线上都不会发生这种情况.
如果我删除了调用CondoBuilding.model.find()
并且只是next()
在体内调用loadCondoList()
此行为不会发生.
我正在运行"keystone": "4.0.0-beta.5"
利用Express 4的Keystone 4"express": "4.14.0"
以下是我在应用中运行的路线的完整列表,如果相关的话:
// Setup Route Bindings
exports = module.exports = function ( app ) {
// Views
app.get( '/', routes.views.index );
app.get( '/condo-list', middleware.loadCondoList, routes.views.condolist );
app.get( '/blog/:category?', routes.views.blog );
app.get( '/blog/post/:post', routes.views.post );
app.get( '/about', routes.views.about );
app.get( '/search', middleware.getAccountType, routes.views.search );
app.all( '/contact', routes.views.contact );
};
Run Code Online (Sandbox Code Playgroud)
CondoList视图:
var keystone = require('keystone');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'condolist';
// Render the view
view.render('condolist');
};
Run Code Online (Sandbox Code Playgroud)
我已经调试了这个问题一段时间,但我最终知道是什么导致了这个问题.任何帮助,将不胜感激.
UPDATE
我按照@ phuhgh的建议,在Express调试模式下运行应用程序.虽然没有立刻突然出现在我身上,但在应用程序启动时我注意到了一些奇怪的事情
以下是正在准备的几条正常行为的序列:
express:router:layer new / +0ms
express:router:route new /blog/post/:post +0ms
express:router:layer new /blog/post/:post +0ms
express:router:route get /blog/post/:post +0ms
express:router:layer new / +0ms
express:router:route new /about +0ms
express:router:layer new /about +0ms
express:router:route get /about +0ms
express:router:layer new / +0ms
express:router:route new /search +1ms
express:router:layer new /search +0ms
express:router:route get /search +0ms
Run Code Online (Sandbox Code Playgroud)
以下是正在准备的公寓列表路线的顺序:
express:router:layer new / +0ms
express:router:route new /condo-list +0ms
express:router:layer new /condo-list +0ms
express:router:route get /condo-list +0ms
express:router:layer new / +0ms
express:router:route get /condo-list +0ms
Run Code Online (Sandbox Code Playgroud)
您可能会注意到,express:router:route get /condo-list +0ms
重复该行.我不知道为什么,但我认为这与我遇到的问题有关.我正在深入研究这个角度,但是再一次,在这个领域有更多知识的人的任何帮助将不胜感激.
更新2 - 堆栈跟踪
我已经调试并逐步完成了堆栈.我可以遵循从功能到功能的路径,一切看起来都很正常,但我的正常基线是查看其他正常工作的路线.老实说,一旦我深入了解Express的内容,我就不知道该寻找什么.
我在浏览堆栈时所做的观察:
/condo-list
路由运行的两次完全相同./about
路由定义更新为以下内容:app.get( '/about', middleware.loadCondoList, routes.views.about );
它正确加载数据并且只运行一次.当我踩过Express lib代码时,有什么我应该特别注意的吗?我感觉有点超出我的深度,我不知道该寻找什么.
经过几天的调试,我终于找到了罪魁祸首,它隐藏在最不可能的地方:视图!
语境
该视图加载了公寓建筑的砖石显示,可以按社区进行过滤。以下是砌体本身的相关片段:
<!-- Condo List Masonry -->
<div class="condo-items">
{{#each condoBuildings}}
<div class="condo-item {{neighborhood.key}}">
<div class="condo-thumb">
<span class="condo-tag tag-art">{{neighborhood.name}}</span>
<a href="/{{condoUrl}}"><img src="{{{cloudinaryUrl image}}}" alt="{{name}}" /></a>
</div>
<div class="condo-body">
<h3><a class="condo-name" href="#">{{name}}</a></h3>
<p>{{condoDescription}}</p>
</div>
</div>
{{/each}}
</div>
Run Code Online (Sandbox Code Playgroud)
该问题是由cloudinaryUrl
这一行的帮助程序引起的:
<a href="/{{condoUrl}}"><img src="{{{cloudinaryUrl image}}}" alt="{{name}}" /></a>
这是帮助程序代码:
_helpers.cloudinaryUrl = function (context, options) {
// if we dont pass in a context and just kwargs
// then `this` refers to our default scope block and kwargs
// are stored in context.hash
if (!options && context.hasOwnProperty('hash')) {
// strategy is to place context kwargs into options
options = context;
// bind our default inherited scope into context
context = this;
}
// safe guard to ensure context is never null
context = context === null ? undefined : context;
if ((context) && (context.public_id)) {
options.hash.secure = keystone.get('cloudinary secure') || false;
var imageName = context.public_id.concat('.', context.format);
return cloudinary.url(imageName, options.hash);
}
else {
return null;
}
};
Run Code Online (Sandbox Code Playgroud)
问题
一些 CondoBuilding 模型尚未定义image
。这导致方法context
中的参数_helpers.cloudinaryUrl
为undefined
。在这种情况下,帮助者将会返回null
。我仍然不确定为什么这会导致页面重新加载,但我确信这是罪魁祸首。
修复方法
更新 Handlebars 模板,以便仅<img>
在 CondoBuilding 模型上存在图像时渲染元素。更新后的模板代码如下所示:
<!-- Condo List Masonry -->
<div class="condo-items">
{{#each condoBuildings}}
<div class="condo-item {{neighborhood.key}}">
<div class="condo-thumb">
<span class="condo-tag tag-art">{{neighborhood.name}}</span>
<a href="/{{condoUrl}}">{{#if image}}<img src="{{{cloudinaryUrl image}}}" alt="{{name}}" />{{/if}}</a>
</div>
<div class="condo-body">
<h3><a class="condo-name" href="#">{{name}}</a></h3>
<p>{{condoDescription}}</p>
</div>
</div>
{{/each}}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,将块添加{{#if image}}
到模板后,路线仅运行一次!
下一步
对此实现的改进是对所有未定义的 CondoBuildings 使用占位符图像image
。我很快就会添加此功能,但我无法抗拒更新答案,因为几天来我一直在努力解决这个问题。
感谢大家的时间和关注。
归档时间: |
|
查看次数: |
280 次 |
最近记录: |