Handlebars:??????????“??”,?????????“?????”
Posted
技术标签:
【中文标题】Handlebars:访问已被拒绝解析属性“名称”,因为它不是其父级的“自己的属性”【英文标题】:Handlebars: Access has been denied to resolve the property "name" because it is not an "own property" of its parent 【发布时间】:2020-06-12 22:52:30 【问题描述】:我遇到了车把 4.7.3 的问题。我已经从这个问题检查了解决方案, Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent 但这不是我的代码的解决方案,所以我希望有人可以帮助我。
Controller.js
submitPosts: (req, res) =>
// Check the attributs from create.handlebars for success or error message
const newPost = new Post(
surname: req.body.surname,
name: req.body.name,
biography: req.body.biography,
profilpicture: req.body.profilpicture,
paintings: req.body.paintings,
);
// Safe new posts
newPost.save().then(post =>
console.log(post);
flash('success-message', 'new post!')
res.redirect('/admin/posts');
);
,
postModel.js
const
mongoose = require('mongoose');
const Schema = mongoose.Schema; // get props
const PostSchema = new Schema(
// define props --> required for a post
surname:
type: String,
default: ''
,
name:
type: String,
default: ''
,
biography:
type: String,
default: ''
,
profilpicture:
type: Object,
,
paintings :
type: Object,
);
module.exports = mongoose.model('post', PostSchema);
index.handlebars
#each post
<tr>
<td> surname </td>
<td> name </td>
<td><img style="width: 100px; height:100px;" src=" profilpicture "></td>
<td> biography </td>
<td><img style="width: 100px; height:100px;" src=" paintings "></td>
</tr>
/each
已经尝试了其他关于堆栈溢出、其他车把版本、更改路由器代码的其他问题的所有可能性,......没有任何工作:(
【问题讨论】:
handlebarsjs.com/api-reference/… 【参考方案1】:刚刚遇到了同样的问题,让我很紧张。 尝试了其他软件包等,但最后一个简单的命令解决了这个问题。 它的命令“.lean()” 在这里找到更多:link to mongoose docs for lean()
我的代码示例:
// show message page Route
router.get("/", (req, res) =>
//read all Message entries from db
Message.find()
//sort them descending by date property
.sort( date: "desc" )
.lean()
//take these and call the rendered page and pass them as object to the page
.then(messages =>
res.render("messages/show_messages", messages: messages );
);
);
【讨论】:
@Dariun 是的,在你的路线中使用它。我的示例在路由器文件中使用。因此,“router.get...”如果您不使用单独的路由器文件,请使用“app.get...”路由中的“.lean()”函数。不要复制我的路线。在您的路线中插入“.lean()”以上是关于Handlebars:??????????“??”,?????????“?????”的主要内容,如果未能解决你的问题,请参考以下文章
Handlebars的使用方法文档整理(Handlebars.js)
?? Handlebars 'each' ?????????
Handlebars:??????????“??”,?????????“?????”