使用handlebars.js访问mongodb结果

时间:2019-04-27 22:03:53

标签: node.js mongodb

我正在尝试使用把手访问从mongodb传递的对象(heroImage)。目前,我只能使整个结果显示在页面上。我想提取网址,以便可以在img src标签中使用它。

heroImage(我使用{{heroImage}}

时显示的内容
{ _id: 5cc4c546feed023a00528f67, 
    slug: 'test',
     name: 'test', __
     v: 0, 
     description: 'test', 
     image: { 
         public_id: 'b0byc2mappz155vixsk9', version: 1556399446, signature: 'bec9125f0421c15c646bcb6f91116cc43032888a', 
         width: 2048, height: 1463, 
         format: 'jpg',
          resource_type: 'image', 
          url: 'http://res.cloudinary.com/', 
          secure_url: 'https://res.cloudinary.com/' 
          } 
        }

hbs文件,该文件应获取heroImage并打印其URL

{{#if galleries}}
    {{#each galleries}}


    <h2>{{name}}
        <div class="pull-right text-muted">19th May 2014</div>
    </h2>
    <div class="row gallery-images">
        <p>{{url}}</p>
            {{#if heroImage}}
            <div class="col-sm-4 col-md-4 gallery-image1">
                <h3>url<:{{heroImage.url}}/h3>

            </div>
            {{/if}}

    </div>



    {{/each}}
    {{else}}

<!-- else -->
<h3 class="text-muted">There are no image galleries yet.</h3>
{{/if}}

我的index.js文件,正在寻找附加到图库模型的特定heroImage

var keystone = require('keystone'),                                                                                                                   
Gallery = keystone.list('Gallery'),                                                                                                               
Image = keystone.list('Image');                                                                                                                   

exports = module.exports = function (req, res) {                                                                                                      

    var view = new keystone.View(req, res);                                                                                                       
    var locals = res.locals;                                                                                                                      

    // locals.section is used to set the currently selected                                                                                       
    // item in the header navigation.                                                                                                             
    locals.section = 'home';                                                                                                                      
    locals.galleries = [];                                                                                                                        

    //Loading the galleries                                                                                                                       
    view.on('init', function(next){                                                                                                               

        var q = Gallery.model.find()                                                                                                              
           .populate('heroImage images')                                                                                                          
           .sort('sortOrder');                                                                                                                    

           q.exec(function(err, results) {                                                                                                        

               locals.galleries = results;                                                                                               
               next(err);                                                                                                                         
           });                                                                                                                                    
    });                                                                                                                                           


    // Render the view                                                                                                                            
    view.render('index');                                                                                                                         
};

0 个答案:

没有答案