车把:拒绝访问以解析属性"from"的属性.因为它不是“私有财产"它的父母 [英] Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent

查看:87
本文介绍了车把:拒绝访问以解析属性"from"的属性.因为它不是“私有财产"它的父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有手柄的Nodejs后端和服务器端渲染. 从车把读取对象的doc数组后,该数组包含键"content"和"from". 但是,当我尝试使用#each遍历对象数组时, 出现错误车把:拒绝访问以解析属性"来自",因为它不是其父项的自有属性".

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.

我尝试将我在doc数组中获取的数据进行console.log(),一切似乎都很好.

I've tried to console.log() the data that I've fetched in the doc array and everything seems fine.

从某种角度来说,这是猫鼬查询,
我已将对象doc作为键添加到res.render参数中.

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

这是我要遍历的.hbs文件的一部分:

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

推荐答案

我通过为车把安装开发依赖项来解决此问题

i solve this issue by installing a dev dependency for handlebars

npm i -D handlebars@4.5.0

这篇关于车把:拒绝访问以解析属性"from"的属性.因为它不是“私有财产"它的父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆