简体   繁体   中英

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. I've tried running with readdir sync, and that doesn't seem to work either. Is it something to do with the nested if statements? 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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