Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  82] [ 6]  / answers: 1 / hits: 58561  / 4 Years ago, sat, january 11, 2020, 12:00:00

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}}




More From » node.js

 Answers
25

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
});

[#51315] Friday, January 3, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
juancarlos

Total Points: 580
Total Questions: 105
Total Answers: 103

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
;