如何使用把手从数据库中提供div id?

时间:2018-02-22 06:57:59

标签: node.js express handlebars.js

所以我正在用hbs模板编写节点代码。我想从数据库中给出div id,你可以在下面的hbs代码的<a href= "#{{content1[0].content_heading}}" class="more">Learn More</a>行中看到。问题是当我传递像"#{{content1}}"这样的整个对象时,它正在工作并将整个对象作为数据传递。

但当我使用content1[0]定位第0个元素时,它显示错误错误:

Error: E:\Monter\Project\node.js\third-page\full-project\views\basic-page.hbs: Parse error on line 35:
...iv>          <a href= "#{{content1[0].content_
----------------------^

节点代码:

// TO GET 'id' data in HBS for BASIC.
app.get('/:id/basic', (req, res) => {
  var id = req.params.id;
  var encoded = encodeURI(id);

  console.log(id);

  basicModel.findOne({"basic_page.navbar_header":id}).then((BasicModel) => {
    if (BasicModel === null) {
      res.send('Sorry, the page you have requested does not exsist.');
      return res.status(404).send();
    }
    console.log(typeof(BasicModel.basic_page.content1[0].content_heading));
    res.render('basic-page.hbs', {
      id: encoded,
      down_arrow_link: BasicModel.basic_page.content1[0].content_heading,
      navbar_header: BasicModel.basic_page.navbar_header,
      header_image_name: BasicModel.basic_page.header_image_name,
      content1: BasicModel.basic_page.content1,
      content2: BasicModel.basic_page.content2
    });
    // console.log(DescriptionModel);

  }).catch((e) => {
    res.status(400).send();
  });
});

HBS代码:     

<section id="banner" class="bg-img" data-bg="{{id}}/{{header_image_name}}">
    <div class="inner">
        <header>
            <h1>Basic Skill</h1>
        </header>
    </div>
    <a href= "#{{content1[0].content_heading}}" class="more">Learn More</a>
</section>

架构:

var basicSchema = new Schema({
  basic_page: {
      navbar_header: {type: String, required: true, trim: true, minlength: 1, index: ({header:1}, {unique: true}), unique: true},
      header_image_name: {type: String, required: true, trim: true},
      content1: [
        {
          content_heading: {type: String, required: true, trim: true},
          content_data: {type: String, required: true, trim: true},
          subcontent: [
            {
              subcontent_heading: {type: String, required: true, trim: true},
              subcontent_data: {type: String, required: true, trim: true},
              subcontent_image_name: {type: String, required: true, trim: true}
            }
          ]
        }
      ],
      content2: [
        {
          content_display_number: {type: Number, required: true, trim: true},
          content_heading: {type: String, required: true, trim: true},
          content_data: {type: String, required: true, trim: true},
          subcontent_image_name: {type: String, required: true, trim: true}
        }
      ]
    }
  })

2 个答案:

答案 0 :(得分:0)

试试这个

<a href= "#{{content1.0.content_heading}}" class="more">Learn More</a>

答案 1 :(得分:0)

应为<a href= "#{{content1.[0].content_heading}}" class="more">Learn More</a>

您错过了数组索引.之前的点[0](与JS语法不同)。

希望这有帮助。