从 Handlebars 4.6.0 开始,模板不能再访问原型属性和方法(默认情况下)。 有关讨论、原因和理由,请参阅#1633。
有恢复和控制旧行为的运行时选项(请参阅 https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access ),但如果您使用的是框架而不是Handlebars 核心,可能不清楚如何设置它们。
如果您收到此错误消息,请添加包含以下信息的评论
express-hbs
、 express-handlebars
)正在调用 Handlebars? (如果不确定,请粘贴npm ls handlebars
或yarn why handlebars
的输出)。Handlebars.compile
和生成的模板函数,您的答案应该在这里https://handlebarsjs.com/api-reference/runtime-options.html#options -to-control-prototype-access。 如果文档没有回答您的问题或您不理解它,请询问(此处)以便改进文档。express-handlebars
与mongoose
结合使用Mongoose 的模型是类,属性不是父对象的“自己的属性”。
最干净的方法是确保把手输入是正确的纯 JavaScript 对象。 这可以在 Mongoose 中通过调用toJSON()
或toObject
来完成
app.get('/test', function (_req, res) {
Kitten.find({}).then(kittens => {
res.render('test.hbs', {
kittens: kittens.map(kitten => kitten.toJSON())
})
})
});
文档中提到的runtimeOptions
(或templateOptions
)不能在这里使用,因为express-handlebars
不支持设置它们。
快速而肮脏的黑客(这真的很hacky,我不建议这样做)是使用
var handlebarsInstance = Handlebars.create();
handlebarsInstance.compile = function (templateStr, compileOptions) {
// compile template
// return a function that calls the compiled template with `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options
// WARNING: People can crash your system by frabricating special templates. Don't use
// this if your users can create templates.
}
app.engine('hbs', expressHandlebars({
handlebars: handlebarsInstance
}));
如果您想要一个Handlebars.createWithAllowedProtoAccess
或类似的东西,请在此评论中添加一个火箭表情符号。
我有这个问题,我不知道如何解决它,你能帮我吗?
谁能告诉我在使用车把时如何解决此错误?
错误:
En-US:车把:拒绝访问以解析属性“名称”,因为它不是其父级的“自己的属性”。
您可以添加运行时选项以禁用验证或此警告:
有谁知道当您使用车把时如何报告此错误?
错误:
En:车把:拒绝访问以解析“名称”属性,因为它不是您父亲的“自己的属性”。
您可以添加运行时选项以禁用验证或此警告:
我的是这样的:
const 车把 = 要求('express-handlebars')
app.engine('handlebars',handlebars({defaultLayout: 'main'}))
app.set('视图引擎', '车把')
app.get('/payment', function(req, res){
Payment.findAll().then(function(payments){
res.render('payment', {payments: 付款})
})
我想使用增强功能。 在
我想使用增强功能。 br
看看这个: https: //www.npmjs.com/package/@handlebars/allow -prototype-access
文档中提到的
runtimeOptions
(或templateOptions
)不能在这里使用,因为express-handlebars
不支持设置它们。
由于 express-handlebars 不允许您设置这些运行时配置选项,是否有人知道强制 express-handlebars 使用车把 4.5.3 的方法?
您可以将https://www.npmjs.com/package/@handlebars/allow -prototype-access 与express-handlebars
一起使用。 它包装已编译的模板,以便设置默认参数。
嗨 Nils,我无法让该程序包工作,当我尝试使用该程序包运行时,我收到“错误:模块“车把”不提供视图引擎。” 错误。 似乎另一个用户在https://github.com/wycats/handlebars.js/issues/1648 中遇到了类似的问题。 我在同一个线程中发布了我的结果。 谢谢
@craj1031tx我在 #1648 中回答。
框架:
express-handlebars
与mongoose
结合使用Mongoose 的模型是类,属性不是父对象的“自己的属性”。
最干净的方法是确保把手输入是正确的纯 JavaScript 对象。 这可以在 Mongoose 中通过调用
toJSON()
或toObject
来完成app.get('/test', function (_req, res) { Kitten.find({}).then(kittens => { res.render('test.hbs', { kittens: kittens.map(kitten => kitten.toJSON()) }) }) });
文档中提到的
runtimeOptions
(或templateOptions
)不能在这里使用,因为express-handlebars
不支持设置它们。快速而肮脏的黑客(这真的很hacky,我不建议这样做)是使用
var handlebarsInstance = Handlebars.create(); handlebarsInstance.compile = function (templateStr, compileOptions) { // compile template // return a function that calls the compiled template with `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options // WARNING: People can crash your system by frabricating special templates. Don't use // this if your users can create templates. } app.engine('hbs', expressHandlebars({ handlebars: handlebarsInstance }));
如果您想要一个
Handlebars.createWithAllowedProtoAccess
或类似的东西,请在此评论中添加一个火箭表情符号。
多谢,伙计 :)
我把火箭留在那儿!
我正在使用带有 Sequelize 的 Express-Handlebars。
npm ls 车把:
+-- [email protected]
| `-- [email protected]
`-- [email protected]
控制台输出:
Handlebars: Access has been denied to resolve the property "name" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "id" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "email" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "url" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "description" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "createdAt" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "updatedAt" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
我会用你的猫鼬解决方案试试你的
框架:
express-handlebars
与mongoose
结合使用Mongoose 的模型是类,属性不是父对象的“自己的属性”。
最干净的方法是确保把手输入是正确的纯 JavaScript 对象。 这可以在 Mongoose 中通过调用
toJSON()
或toObject
来完成app.get('/test', function (_req, res) { Kitten.find({}).then(kittens => { res.render('test.hbs', { kittens: kittens.map(kitten => kitten.toJSON()) }) }) });
文档中提到的
runtimeOptions
(或templateOptions
)不能在这里使用,因为express-handlebars
不支持设置它们。快速而肮脏的黑客(这真的很hacky,我不建议这样做)是使用
var handlebarsInstance = Handlebars.create(); handlebarsInstance.compile = function (templateStr, compileOptions) { // compile template // return a function that calls the compiled template with `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options // WARNING: People can crash your system by frabricating special templates. Don't use // this if your users can create templates. } app.engine('hbs', expressHandlebars({ handlebars: handlebarsInstance }));
如果您想要一个
Handlebars.createWithAllowedProtoAccess
或类似的东西,请在此评论中添加一个火箭表情符号。
我们也可以使用lean() 方法,如下所示
app.get('/test', function (_req, res) { Kitten.find().lean().then(kittens => res.render('test.hbs', { kittens: kittens})); });
由于 express-handlebars 不允许您设置这些运行时配置选项,是否有人知道强制 express-handlebars 使用车把 4.5.3 的方法?
我创建了一个 PR 以允许向 express-handlebars 添加运行时配置选项。 https://github.com/express-handlebars/express-handlebars/pull/53
最有用的评论
框架:
express-handlebars
与mongoose
结合使用Mongoose 的模型是类,属性不是父对象的“自己的属性”。
最干净的方法是确保把手输入是正确的纯 JavaScript 对象。 这可以在 Mongoose 中通过调用
toJSON()
或toObject
来完成文档中提到的
runtimeOptions
(或templateOptions
)不能在这里使用,因为express-handlebars
不支持设置它们。快速而肮脏的黑客(这真的很hacky,我不建议这样做)是使用
如果您想要一个
Handlebars.createWithAllowedProtoAccess
或类似的东西,请在此评论中添加一个火箭表情符号。