I'm trying to access an object (heroImage)passed from my mongodb using handlebars. At the moment I can only get the whole result to show on my page. I'm wanting to extract the url so I can use it in a img src tag.

heroImage (what's displayed when i use {{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 file, that should be getting the heroImage, and printing its 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}}

my index.js file, that's looking for the particular heroImage attached to the gallery model

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 Answers