【问题标题】:Javascript: Call function inside Node.js objectJavascript:调用 Node.js 对象内的函数
【发布时间】:2016-05-19 09:40:49
【问题描述】:

我要调用这个异步回调函数:

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()

在这个 Node.js 路由器内部:

/* 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()
  });
});

我希望在上面的示例中将这个预期的输出放在images: 之后(在控制台中显示):

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

我该怎么做?

我也有这个车把文件:

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

有时在 html 输出中呈现的输出是:

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

但我想要:

<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>

【问题讨论】:

  • globAsync 是否在不同的模块中?
  • 我按照这里的教程来获取异步回调的结果:github.com/maxogden/art-of-node#callbacks我是 JS 新手。
  • 您应该将globResults 设为globCaller 的参数(以及callback 调用的参数)。
  • … images: globCaller() … 没有意义。
  • 如果要数组,为什么要通过JSON.stringify()传递结果?只需删除该行。

标签: javascript node.js object express asynchronous


【解决方案1】:

在 glob() 的回调中使用 res.render() 比使用所有全局变量更简单:

/* 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
        });

    });
});

编辑:评论中讨论的替代解决方案

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
        });
    });
});

【讨论】:

  • 抱歉,工作中!真的没有办法在全局变量中定义吗?将整个路由器放在一个函数中感觉不对。正如@Juhana 所说,我也必须删除JSON.stringify()
  • 是的,您可以单独定义调用,也可以使用回调调用它。我会把它添加到答案中。
  • 谢谢!完美运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
相关资源
最近更新 更多