Handlebars:访问已被拒绝解析属性“from"因为它不是“自有财产"其父 [英] Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent
问题描述
我正在使用带有使用把手的服务器端渲染的 Nodejs 后端.从车把读取 doc
对象数组后,其中包含关键的内容"和来自".但是,当我尝试使用 #each
循环遍历对象数组时,出现错误Handlebars: Access has been denied to resolve the property "from" because it is not a " own property" of its parent"出现.
我尝试将我在 doc 数组中获取的数据进行 console.log() 处理,一切似乎都很好.
从某些角度来看,这是猫鼬查询,
我在 res.render 参数中添加了对象 doc 作为键.
Confession.find().sort({日期:-1}).then(功能(文档){for(var i=0; i < doc.length; i++){//检查发件人是否匿名if (doc[i].from === "" || doc[i].from == null){doc[i].from = "匿名";}//为格式化日期添加额外的JSON字段doc[i].formattedDate = formatTime(doc[i].date);}res.render('index', {title: 'Confession Box', success:req.session.success, errors: req.session.errors, 忏悔: doc});req.session.errors = null;req.session.success = null;});
这是我试图遍历的 .hbs 文件部分:
{{#每个忏悔}}<div class="uk-card uk-card-default uk-card-body uk-margin uk-align-center uk-width-1-2@m" ><div class="uk-text-bold">消息:</div><div>{{this.content}}</div><div>发件人:{{this.from}}</div><div>发布:{{this.formattedDate}}</div>
{{/each}}
如果使用 mongoose,可以通过使用 .lean() 获取 json 对象(而不是 mongoose 对象)来解决此问题:
dbName.find({}).lean()//执行查询.exec(function(error, body) {//一些代码});
I am using a Nodejs backend with server-side rendering using handlebars.
After reading a doc
array of objects from handlebars, which contains key "content" and "from".
However when I try to use #each
to loop through the array of objects,
the error "Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent" appears.
I've tried to console.log() the data that I've fetched in the doc array and everything seems fine.
For some perspective, this is the mongoose query,
I've added the object doc as a key inside the res.render arguments.
Confession.find()
.sort({date: -1})
.then(function(doc){
for(var i=0; i < doc.length; i++){
//Check whether sender is anonymous
if (doc[i].from === "" || doc[i].from == null){
doc[i].from = "Anonymous";
}
//Add an extra JSON Field for formatted date
doc[i].formattedDate = formatTime(doc[i].date);
}
res.render('index', {title: 'Confession Box', success:req.session.success, errors: req.session.errors, confession: doc});
req.session.errors = null;
req.session.success = null;
});
This is the portion of .hbs file I am trying to loop through:
{{#each confession}}
<div class="uk-card uk-card-default uk-card-body uk-margin uk-align-center uk-width-1-2@m" >
<div class="uk-text-bold">Message: </div>
<div>{{this.content}}</div>
<div>From: {{this.from}}</div>
<div>Posted: {{this.formattedDate}}</div>
</div>
{{/each}}
If using mongoose, this issue can be solved by using .lean() to get a json object (instead of a mongoose one):
dbName.find({}).lean()
// execute query
.exec(function(error, body) {
//Some code
});
这篇关于Handlebars:访问已被拒绝解析属性“from"因为它不是“自有财产"其父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!