(Handlebars / JSON)嵌套每个读取数组

时间:2018-02-04 23:49:56

标签: json node.js handlebars.js

我有一个family - 表,其中属性member为数组。

第一个#each工作正常,但我无法使第二个/嵌套#each工作从family - 表中的数组中读取数据(请参阅JSON部分)。

我尝试了这两种方法但没有成功:

  {{#each family}}                   |      {{#each family as |x|}}
    <h1>{{surName}}</h1>             |         <h1>{{x.surName}}</h1>
                                     |
    {{#each member}}                 |         {{#each x.member as |y|}}
       <h1>{{firstName}}</h1>        |            <h1>{{y.firstName}}</h1>
    {{else}}                         |         {{else}}
       <h1>Not working</h1>          |            <h1>Not working</h1>
    {{/each}}                        |         {{/each}} 
                                     |
  {{else}}                           |       {{else}}
      <h1>No family</h1>             |           <h1>No family</h1>
  {{/each}}                          |       {{/each}}

JSON:

//family collection
{
    "_id": {
        "$oid": "5a6a906492b7b30014b18111"
    },
    "date": {
        "$date": "2018-01-26T02:20:20.428Z"
    },
    "surName": "x",
    "members": [
        {
            "firstName": "x"
        },
        {
            "firstName": "y"
        },
        {
            "firstName": "z"
        }
    ]
}

路线:

const express = require("express");
const mongoose = require("mongoose");
const router = express.Router();

    require("../models/Family");
    const Family = mongoose.model("family");

    router.get('/', (req, res) => {
        Family.find({}) 
        .sort({ date: 'desc' })
        .then(family => {
          res.render('family/index', {
            family: family
          });
        });
    });    
module.exports = router;

编辑:

My Family.js Model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//Create Schema
const FamilySchema = new Schema({
  surName: {
    type: String,
    required: true
  },
  firstName: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});

mongoose.model('family', FamilySchema);

1 个答案:

答案 0 :(得分:2)

你需要改变两个地方

  1. 您的家庭收藏必须是array,而不是object
  2. 数据字段为members而非member
  3. 以下代码可以使用

    app.js

    var express = require('express');
    var exphbs  = require('express-handlebars');
    var app = express();
    
    app.engine('handlebars', exphbs());
    app.set('view engine', 'handlebars');
    
    app.get('/', function (req, res) {
        const family = [{
          "_id": {
              "$oid": "5a6a906492b7b30014b18111"
          },
          "date": {
              "$date": "2018-01-26T02:20:20.428Z"
          },
          "surName": "x",
          "members": [
              {
                  "firstName": "x"
              },
              {
                  "firstName": "y"
              },
              {
                  "firstName": "z"
              }
          ]
      }]
    
        res.render('home', {
          family
        });
    });
    
    app.listen(3000);
    

    home.handlebars

    {{#each family as |family|}}                  
        <h1>{{family.surName}}</h1>           
    
        {{#each family.members as |member|}}                
           <h1>{{member.firstName}}</h1>       
        {{else}}                        
           <h1>Not working</h1>         
        {{/each}}                        
    
      {{else}}                          
          <h1>No family</h1>             
    {{/each}}