简体   繁体   English

如何强制一个带有嵌套 if 语句的代码块在 node.js 中的后续代码之前完成?

[英]How can I force a one block of code with nested if statements to finish before subsequent code in node.js?

In the following block of code, I want all the statements below readdir to execute before the final statement, so that the final console log includes the entire database.在下面的代码块中,我希望 readdir 下面的所有语句在最终语句之前执行,以便最终控制台日志包含整个数据库。 I've tried running with readdir sync, and that doesn't seem to work either.我试过使用 readdir sync 运行,但这似乎也不起作用。 Is it something to do with the nested if statements?这与嵌套的 if 语句有关吗? The last line executes before any of the other code, and I'd like to circumvent that.最后一行在任何其他代码之前执行,我想绕过它。

// Load node.js filesystem module
var fs = require('fs');
// Load cheerio module for HTML scraping
const cheerio = require('cheerio');
// Load find and replace module
const replace = require('replace-in-file');
// Make for loop that cycles through all files in folder
var stream = fs.createWriteStream("my_file.txt");

var database = {};

const CurrentFolder = './';
fs.readdir(CurrentFolder, (err, files) => {
    files.forEach(file => {
        // only modify files that contain 'page' and end in .html
        if (file.includes('.html')) {
            if (file.includes('page')) {
                console.log(file + ' includes html');
                fs.readFile(file, 'utf8', function (err, contents) {
                    if (err) console.log('ERROR: ' + err);
                    // only modify files that contain a '.page-title-lvl-cover' heading
                    if (contents.includes('page-title-lvl-cover')) {
                        var x = contents;
                        // console.log(x);
                        const $ = cheerio.load(contents)
                        // Extract page title (found within page-title-lvl-cover class)
                        const result = $('.page-title-lvl-cover').text()



                        database[file] = result;

                        console.log(database);

                        console.log(result)


                    }
                });
            }
        }
    });
});
console.log('Last ' + database)

Just make everything synchronize, including readdir and readFile .只需使所有内容同步,包括readdirreadFile

// Load node.js filesystem module
var fs = require('fs');
// Load cheerio module for HTML scraping
const cheerio = require('cheerio');
// Load find and replace module
const replace = require('replace-in-file');
// Make for loop that cycles through all files in folder
var stream = fs.createWriteStream("my_file.txt");

var database = {};

const CurrentFolder = './';
const files = fs.readdirSync(CurrentFolder);
files.forEach(file => {
    // only modify files that contain 'page' and end in .html
    if (file.includes('.html')) {
        if (file.includes('page')) {
            console.log(file + ' includes html');
            const contents = fs.readFileSync(file, 'utf8');
            // only modify files that contain a '.page-title-lvl-cover' heading
            if (contents.includes('page-title-lvl-cover')) {
                var x = contents;
                // console.log(x);
                const $ = cheerio.load(contents)
                // Extract page title (found within page-title-lvl-cover class)
                const result = $('.page-title-lvl-cover').text()
                database[file] = result;
                console.log(database);
                console.log(result)
            }
        }
    }
});
console.log('Last ' + database)

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

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