[英]app.render [TypeError: Cannot set property 'content' of undefined] Using Express with Gaikan in Node.js
res.render is working perfectly: res.render运行完美:
res.render('main', function(err, html){
// Returns '<html></html>' contained inside of 'views/main.html'
console.log(html);
});
But my app.render: 但是我的app.render:
app.render('main', function(err, html){
// Returns undefined
console.log(html);
});
The error being returned by app.render is: app.render返回的错误是:
[TypeError: Cannot set property 'content' of undefined]
I assume it's trying to set data.content (being part of Gaikan), but I don't know for certain, and I don't know what the problem is. 我假设它正在尝试设置data.content(属于Gaikan),但是我不确定,也不知道问题出在哪里。
Help would be appreciated. 帮助将不胜感激。
Stack trace: http://puu.sh/5qGSq.png 堆栈跟踪: http : //puu.sh/5qGSq.png
Config: 配置:
var express = require('express'),
gaikan = require('gaikan');
app.configure(function() {
app.engine('html', gaikan);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
gaikan.options.scoped = true;
});
As stated above, the template is just <html></html>
. 如上所述,模板只是
<html></html>
。
I think this is an issue of Gaikan making an assumption that isn't true, namely that the options
argument – which is passed to the render function – always contains a _locals
property. 我认为这是Gaikan做出的假设不正确的问题,即,传递给render函数的
options
参数始终包含_locals
属性。
Looking at the Express code, that property is only guaranteed to exist when calling res.render()
, but when you call app.render()
you need to explicitly pass it yourself before Gaikan works: 查看Express代码,只有在调用
res.render()
时,该属性才能保证存在,但是当您调用app.render()
您需要在Gaikan工作之前自己明确地传递它:
app.render('main', { _locals : {} }, function(err, html) { ... });
The reason why I couldn't reproduce it is because this issue only occurs when gaikan.options.scoped === true
. 我之所以无法复制它,是因为仅当
gaikan.options.scoped === true
时才会出现此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.