NodeJS Handlebars:解析属性“imagePath”的访问被拒绝,因为它不是其父级的“自己的属性”

z5btuh9x  于 2023-06-22  发布在  Node.js
关注(0)|答案(4)|浏览(99)

我不能得到我保存的数据从我的产品播种到商店正确
看起来像这个https://prnt.sc/t10v00
我想从我的数据图像和标题来到页面
在我的终端上写着//// Handlebars:解析属性“title”的访问被拒绝,因为它不是其父级的“自己的属性”

{{# each products}}

<div class="row">
{{# each this}}

<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{this.imagePath}}" alt="..." class = "img-responsive">
<div class="caption">
<h3 align="center">{{this.title}}</h3>
<p class="description">{{this.description}}</p>
    <div class="clearfix">
    <div class="price pull-left">€{{this.price}}<ahref="#" style="float:right"class="btn btn-primary pull-right" role="button">Add cart</a> </div>

     </div>
</div>

</div>

{{/each}}

</div>

{{/each}}

index.js

var express = require('express');
var router = express.Router();
var Product = require('../models/product');
/* GET home page. */
router.get('/', function(req, res, next) {
 Product.find(function(err, docs) {
var productChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
  productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
 });
});

module.exports = router;

有谁能帮我吗?

x6yk4ghg

x6yk4ghg1#

"express-handlebars": "^3.0.0"

使用这个版本。它起作用了。它为较新的版本提供了相同的输出。

5cg8jx4n

5cg8jx4n2#

遵循下面给出的语法:

dbName.find({}).lean()
  // execute query
  .exec(function(error, body) {
    //Execute you code
  });
k97glaaz

k97glaaz3#

这段代码适合我:

const Handlebars = require('handlebars');
const expressHandlebars=require('express-handlebars');
const { allowInsecurePrototypeAccess } = require('@handlebars/allow-prototype-access');

// view engine setup
app.engine('.hbs', expressHandlebars({ handlebars: allowInsecurePrototypeAccess(Handlebars) ,defaultLayout: 'layout', extname: '.hbs'}));
//app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '.hbs');
g2ieeal7

g2ieeal74#

I've added the following code and it's working fine. You need to add the runtimeoptions here. 
    const {engine} =  require('express-handlebars');
      
        
    app.engine('.hbs', engine({
            defaultLayout: 'main', 
            extname: 'hbs',
            runtimeOptions: {
                allowProtoPropertiesByDefault: true,
                allowProtoMethodsByDefault: true
            }
          }));
    app.set('view engine', '.hbs');

I have used the above code to implement the .hbs template engine.

相关问题