Eddy.G
Eddy.G

Reputation: 11

how i can render mongodb data with my view

i am using handlebars as my view and mongodb as my database, i am trying to get data from my database and render it on my page (shop.handlbars) but nothing shows. this is the route code for the page.

router.get('/shop', (req, res) => {
Product.find({}, (err, products) => {
    if(!err) {
        console.log(products);
        res.render('shop', {products: products});
    }else {
    console.log(err);
    }
})

and on my shop.handlebars view i tried displaying it like this:

{{#each products}}
        {{!-- <div class=" prdbox rounded p-2">
            <h4 class="product-name">product_name</h4>
            <div class="manufacturer">manufacturer</div>
            <div class="price"><b>Nprice</b></div>
            <div class="imgcont rounded">

            </div>
            <div class="btncont" >
                <a href="/users/cart" class=" cartbtn rounded">Add to cart</a>
                <a href="/products/buy" class=" buybtn rounded">Buy</a>
            </div>
        </div> --}}
        {{/each}}

Please what am i getting wrong?

Upvotes: 1

Views: 77

Answers (1)

Dmitriy
Dmitriy

Reputation: 1241

You need to add curly braces around the keys of products like shown in the handlebars docs. So for example:

{{#each products}}
        {{!-- <div class=" prdbox rounded p-2">
            <h4 class="product-name">{{product_name}}</h4>
            <div class="manufacturer">{{manufacturer}}</div>
            <div class="price"><b>{{Nprice}}</b></div>
            <div class="imgcont rounded">

            </div>
            <div class="btncont" >
                <a href="/users/cart" class=" cartbtn rounded">Add to cart</a>
                <a href="/products/buy" class=" buybtn rounded">Buy</a>
            </div>
        </div> --}}
        {{/each}}

Upvotes: 1

Related Questions