【问题标题】:gitignore a subfolder using the main gitignore filegitignore 使用主 gitignore 文件的子文件夹
【发布时间】:2017-12-05 15:19:13
【问题描述】:

https://github.com/codyc4321/Flashcard-Generator 中,我有一个文件,我想将 HTML 生成器拆分为它自己的函数。

文件位于js/main.js。这个回调:

function generate_html(cards_array) {
    // https://stackoverflow.com/questions/7083045/fs-how-do-i-locate-a-parent-folder
    var webpage_path = __dirname + '/../index_generated.html';
    var template_path = __dirname + '/../index_template.html';
    var html;
    var html = fs.readFile(template_path, 'utf-8', function(error, source) {
        var template = handlebars.compile(source);
        var data = {
            cards: cardsArr
        }
        return template(data);
    });
    return html
}

返回 undefined 而不是把手生成的 html。如何从这个函数返回 html?

【问题讨论】:

    标签: javascript node.js fs


    【解决方案1】:

    它返回undefined 因为fs.readFile() 是异步的。尝试使用fs.readFileSync() 或使用在fs.readFile() 响应中调用的回调函数。

    function generate_html(cards_array, cb) {
        // https://stackoverflow.com/questions/7083045/fs-how-do-i-locate-a-parent-folder
        var webpage_path = __dirname + '/../index_generated.html';
        var template_path = __dirname + '/../index_template.html';
        fs.readFile(template_path, 'utf-8', function(error, source) {
            var template = handlebars.compile(source);
            var data = {
                cards: cardsArr
            }
            cb(template(data));
        });
    }
    

    cb是回调函数,带参数响应。

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 2022-11-16
      • 2019-01-06
      • 2015-05-23
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多