0

I want to call this asynchronous callback function:

var glob = require('glob');
var globResults = undefined;
function globAsync(callback) {
  glob('*.jpg', { cwd: 'public/portfolio/weddings/', sort: true }, function (err, files) {
    var results = JSON.stringify(files);
    globResults = results;
    callback();
  });
};

function globCaller() {
  var g = globResults;
  console.log('STRING: ' + g);
  return g;
};
globAsync(globCaller); // This will init globCaller()

Inside this Node.js router:

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('portfolio', {
    layout: 'main',
    centering: true,
    titleShown: false,
    title: 'Hi!',
    description: 'Home page of Lantos Istvan Photography',
    keywords: 'wedding,photography,film,lantos,istvan',
    bodyClass: 'horizontal',
    imagesFolder: '\/portfolio\/weddings\/',
    images: globCaller()
  });
});

I want this expected output placed after images: in the upper example (this is show in the console):

["image-1.jpg","image-10.jpg","image-11.jpg","image-12.jpg"]

How can I do this?

I also have this handlebar file:

  {{#each images}}
  <li><img src="{{../imagesFolder}}{{this}}.jpg" alt=""></li>
  {{else}}
  <p class="empty">No content</p>
  {{/each}}

Sometimes the rendered output in the html output is:

<li><img src="/portfolio/weddings/image-1.jpg,image-10.jpg,image-11.jpg,..." alt="" /></li>

But I want:

<li><img src="/portfolio/weddings/image-1.jpg" alt="" /></li>
<li><img src="/portfolio/weddings/image-10.jpg" alt="" /></li>
<li><img src="/portfolio/weddings/image-11.jpg" alt="" /></li>
6
  • Is the globAsync in a different module? Commented Feb 9, 2016 at 12:02
  • I followed the tutorial here to get the asynchronous callback's result outside of itself: github.com/maxogden/art-of-node#callbacks I'm new to JS. Commented Feb 9, 2016 at 12:05
  • You should make globResults a parameter of the globCaller (and an argument of the callback call). Commented Feb 9, 2016 at 12:08
  • … images: globCaller() … makes no sense. Commented Feb 9, 2016 at 12:09
  • 1
    If you want an array, why do you pass the results through JSON.stringify()? Just remove that line. Commented Feb 9, 2016 at 12:09

1 Answer 1

1

It is simpler just to have res.render() in glob()'s callback instead of having all the global variables:

/* GET home page. */
router.get('/', function(req, res, next) {
    glob('*.jpg', { cwd: 'public/portfolio/weddings/', sort: true }, function (err, files) {
        var results = JSON.stringify(files);

        res.render('portfolio', {
            layout: 'main',
            centering: true,
            titleShown: false,
            title: 'Hi!',
            description: 'Home page of Lantos Istvan Photography',
            keywords: 'wedding,photography,film,lantos,istvan',
            bodyClass: 'horizontal',
            imagesFolder: '\/portfolio\/weddings\/',
            images: results
        });

    });
});

EDIT: Alternative solution as discussed in comment

function globAsync(params, callback) {
    glob(params.wildcard || '*.jpg', {
        cwd: params.cwd || 'public/portfolio/weddings/',
        sort: true
    }, function(err, files) {
        if (err) {
            return callback(err);
        }

        // Do anything else with the results (files) if you need to here

        callback(null, files);  // null means no error, return results in callback
    });
}

router.get('/', function(req, res, next) {
    globAsync({
        wildcard: '*.jpg',   // use default in globAsync if not passed in
        cwd: 'public/portfolio/weddings/' // use default in globAsync if not passed in
    }, function(err, results) {
        if (err) {
            return res.send(err);
        }
        res.render('portfolio', {
            layout: 'main',
            centering: true,
            titleShown: false,
            title: 'Hi!',
            description: 'Home page of Lantos Istvan Photography',
            keywords: 'wedding,photography,film,lantos,istvan',
            bodyClass: 'horizontal',
            imagesFolder: '\/portfolio\/weddings\/',
            images: results
        });
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, working! Really there's no way to define in a global variable? Placing the entire router inside a function feels not right. As @Juhana said, I had to remove JSON.stringify() also.
Yes, you can define the call separately and call it too using callback. I'll add it to the answer.
Thank You! Works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.