如何在ejs模板中迭代选定的数据类别?

时间:2019-06-26 00:38:19

标签: html node.js mongoose ejs mongoose-schema

我是node.js的新手,我有作业。我有一个嵌套的类别数据库,并将它与猫鼬连接起来。我还使用了ejs模板来显示数据。在ejs中,foreach不会迭代数据库项。 Ejs用逗号将彼此相邻的相同类别元素写入。我该如何区分它们以及如何达到第三类?

我也尝试循环,但出现异步错误。

在索引中,我只能调用一次数据库。

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

        });
    });
});

router.use('/', (req, res,next) => {

    CategoryModel.find({}, function (err,mainCategories) {

            res.render('', {
                titlePage: title,
                year: year,
                mainCategories: mainCategories,
            });

    });


});

ejs模板和html端


 <section class="container-fluid">

        <% mainCategories.forEach(function(mainCategory){ %>
        <h1>
            <a class="text-info text-decoration-none" href="<%= mainCategory.id %>">
                <%= mainCategory.name %>
            </a>
        </h1>
        <!-- description -->
        <p>
            <%= mainCategory.page_description %>
        </p>

        <!-- subCat part -->
        <div class="card">
            <ul>
                <li>
                        <h3 class="card-header text-danger ">
                                <%= mainCategory.categories.name %>
                            </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="<%= mainCategory.categories.id %>" class="btn text-white bg-info">See Products</a>
                </li>
            </ul>           
        </div> 
        <% }) %>

    </section> 

这是架构


const testSchema = mongoose.Schema;

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;

我想要这样的输出

<li>Clothing</li>
<p>Clothing.description</p>
<li>Jewelry</li>
<p>Jewelry.description</p>
<li>Accessories </li>
<p>Accessories.description</p>

但是我有这个输出

<li>Clothing,jewelry,Accessories</li>
<p>Clothing.description,Jewelry.description,Accessories.description</p>

我如何分隔每个项目?

0 个答案:

没有答案