我如何用lodash迭代嵌套的类别

时间:2019-06-26 21:19:50

标签: node.js lodash ejs mongoose-schema

我是node.js的新手。我有一个类别,我想用lodash在ejs模板上显示该类别。我检查了一些例子,但我做不到。

我尝试了一些category.find()函数,但是我总是得到子类别与<a>a,b,c</a>一样的链接属性

这里是视角

<!-- subCat part -->
                <% mainCategories.forEach(function(mainCategory){ %>
        <div class="card">
            <h3 class="card-header ">
                <a href="<%= mainCategory.categories.id %>" class="text-danger text-decoration-none">
                    <%= mainCategory.categories.name %>
                </a>
            </h3>
            <!-- image -->
            <img src="<%= mainCategory.categories.image %>" class="mx-auto d-block"
                alt="<%= mainCategory.categories.image %>">

            <!-- subCat Description -->
            <p class="card-body text-justify">
                <%= mainCategory.categories.page_description %>
            </p>
            <a href=# class="btn text-white bg-info">See Products</a>
        </div>

        <%}) %>

这里的index.js

router.get('/:id', (req,res,next) =>{
    const id = req.params.id;
    CategoryModel.find({'categories.parent_category_id': id})
    .then(function (mainCategories) {
        res.render('', {
            titlePage: title,
            year: year,
            mainCategories: mainCategories,

        });
    });
});


```

and this is the schema

```js


var rootSchema = new testSchema({
    _id: mongoose.Schema.Types.ObjectId,
    id: String,
    name: String,
    page_description: String,
    page_title: String,
    c_showInMenu: Boolean,
    parent_category_id: String,
    categories: {
        id: String,
        name: String,
        image: String,
        page_description: String,
        page_title: String,
        c_showInMenu: Boolean,
        parent_category_id: String,
        categories: {
            id: String,
            name: String,
            image: String,
            page_description: String,
            page_title: String,
            c_showInMenu: Boolean,
            parent_category_id: String
        }
    }


});

var Category = mongoose.model('CategoryModel', rootSchema, 'categories');
module.exports = Category;

```

How can i show every sub category inside its category items when i click ```category_parent_id```

0 个答案:

没有答案