简体   繁体   English

如何返回此对象,而不是记录它?

[英]How do I return this object, instead of logging it?

I have a static site generator for which I want to list all available partials in the templates/partials folder. 我有一个静态网站生成器,我想为其列出templates/partials文件夹中的所有可用部分。 I need to format the list of partials as the following kind of object: 我需要将部分列表的格式设置为以下类型的对象:

var partials = {
  'header': 'partials/header', // from './templates/partials/header.html'
  'footer': 'partials/footer' // from './templates/partials/footer.html'
};

I've followed this example to put together this node script: 我已经按照以下示例将以下节点脚本放在一起:

'use strict';

/**
 * Dependencies
 */

var fs = require("fs");
var path = require("path");
var p = 'templates/partials';

/**
 * Scan folder and return object with partials
 */

fs.readdir(p, function (err, files) {
  var obj = {};

  if (err) {
    throw err;
  };

  for (var i = 0; i < files.length; i++) {
    obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
  };

  console.log(obj);
});

When executed from the root of the project it logs: 从项目的根目录执行时,它记录:

{ footer: 'partials/footer.html',
  header: 'partials/header.html' }

Which is good, but I want to return it, not log it. 这很好,但是我想返回它,而不是记录它。 Also I get the idea that what I've written is rather clunky. 我也觉得我写的东西很笨拙。 So my question is, how do I return the object, and how do I make this more robust? 所以我的问题是,如何返回对象,以及如何使它更坚固?

You should provide a callback function: 您应该提供一个回调函数:

function readfiles(callback) {
  fs.readdir(p, function (err, files) {
    var obj = {};

    if (err) {
      throw err;
    };

    for (var i = 0; i < files.length; i++) {
      obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
    };

    callback(obj);
  });
}

This would be used as follows: 这将被如下使用:

readfiles(function(files) {
  // Do stuff with files here.
});

Callback functions allow IO to be done asynchronously; 回调函数允许IO异步完成; this often provides a large speedup to code because it does not need to wait for IO to finish. 这通常可以大大提高代码的速度,因为它不需要等待IO完成。 In this case, it is not so useful, but the callback pattern is very common in Node.js and so it's good practice to use it here to. 在这种情况下,它不是那么有用,但是回调模式在Node.js中非常常见,因此在这里使用它是一个好习惯。

var results = fs.readdir(p, function(err, files) {
  var obj = {};

  if (err) {
    throw err;
  };

  for (var i = 0; i < files.length; i++) {
    obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
  };

  // derectly deal with it here
  deal(obj);
});

or you can consider async: 或者您可以考虑异步:

async.waterfall([

    function(cb) {
      fs.readdir(p, cb)
    },
    function(files, cb) {
      for (var i = 0; i < files.length; i++) {
        obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
      }
      cb(null, obj);
    }
  ],
  function(err, obj) {
    if (err) {
      throw err;
    }

    // do somethin else
  }
);

or you want sync return : 或者您想要同步返回:

var files = fs.readdirSync(p);
for (var i = 0; i < files.length; i++) {
  obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
};

// here's your obj
obj;

read the document for more detail: http://nodejs.org/api/fs.html 请阅读文档以获取更多详细信息: http : //nodejs.org/api/fs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM