简体   繁体   English

app.render [TypeError:无法设置未定义的属性'content']在Node.js中与Gaikan一起使用Express

[英]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.

相关问题 未捕获的TypeError:无法在App.render读取null的属性&#39;nameOfCity&#39; - Uncaught TypeError: Cannot read property 'nameOfCity' of null at App.render 在 Node.js 应用程序中使用 Webpack 时出现类型错误“无法读取未定义的属性 'jquery'” - TypeError “Cannot read property 'jquery' of undefined” while using Webpack in Node.js app Node.JS / Express / Mongo:TypeError:无法读取未定义的属性“位置” - Node.JS/Express/Mongo: TypeError: Cannot read property 'location' of undefined Sequelize.js/Node.js/Express.js:Tasks.findAll() 返回一个 TypeError:Cannot read property &#39;findAll&#39; of undefined - Sequelize.js/Node.js/Express.js: Tasks.findAll()returns a TypeError:Cannot read property 'findAll' of undefined Node.js TypeError:无法读取未定义的属性“主机” - Node.js TypeError: Cannot read property 'host' of undefined 类型错误:无法读取未定义的 Node.js 的属性 - TypeError: Cannot read property of undefined Node.js Node.js-TypeError:无法读取未定义的属性“ document” - Node.js - TypeError: Cannot read property 'document' of undefined Node.js中的TypeError:无法读取未定义的属性“ 0” - TypeError in node.js: cannot read property '0' of undefined 未捕获的类型错误:无法读取 node.js 中未定义的属性“通道” - Uncaught TypeError: Cannot read property 'channel' of undefined in node.js 类型错误:无法读取未定义的属性“forEach”(节点 js、stripe、express) - TypeError: Cannot read property 'forEach' of undefined (node js, stripe, express)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM