karepan
karepan

Reputation: 35

Why is my Node.js program skipping the fs.readFileSync function?

I am writing an Ultimate Tic-Tac-Toe program in Node.js and was hoping to allow players to reload an old game. I was using an object to save the board state, so I reasoned that I could export the data with JSON.stringify and re-load it into the game.

Board object constructor:

this._A1 = new Square();
this._A2 = new Square();
this._A3 = new Square();

this._B1 = new Square();
this._B2 = new Square();
this._B3 = new Square();

this._C1 = new Square();
this._C2 = new Square();
this._C3 = new Square();

this._winner = null;
this._lastMove = null;

Square object constructor:

this._X1 = null;
this._X2 = null;
this._X3 = null;

this._Y1 = null;
this._Y2 = null;
this._Y3 = null;

this._Z1 = null;
this._Z2 = null;
this._Z3 = null;

this._winner = null;
this._winningCombination = null;

I was planning to use Object.assign(board, JSON.parse(data)); to re-load the data using the board class as a base, but my program keeps skipping the code surrounding it.

function importPathPrompt() {
    const givenPath = rl.question('\nWhat is the path? ');
    try {
        if (fs.existsSync(path.resolve(givenPath)) && /^.+\.(?:(?:[jJ][sS][oO][nN]))$/.test(givenPath)) {
            return path.resolve(givenPath);
        }
        else {
            return importPathPrompt();
        }
    }
    catch(err) {
        console.error(err);
    }
}

if (importPath) {
    console.log(importPath);
    fs.readFileSync(require.resolve(importPath), (err, data) => {
        console.log('Reading file...');
        if (err) {
            console.log(`Error: ${err}`);
        }
        else {
            console.log(`Trying...\n${board}`);
            Object.assign(board, JSON.parse(data));
            console.log(`Success!\n${board}`);
        }
    });
    console.log('Done?');
}

Is there something wrong with this code? Whenever I run my program, it runs console.log(importPath); and console.log('Done?'); but seems to skip the entire fs.readFileSync function.

Sample save-state JSON file:

{
    "_A1": {
        "_X1": "X",
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": "O",
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_A2": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": "O",
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_A3": {
        "_X1": null,
        "_X2": "O",
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_B1": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": "O",
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_B2": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_B3": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_C1": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": null,
        "_winner": null,
        "_winningCombination": null
    },
    "_C2": {
        "_X1": null,
        "_X2": "X",
        "_X3": "X",
        "_Y1": null,
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": "X",
        "_winner": null,
        "_winningCombination": null
    },
    "_C3": {
        "_X1": null,
        "_X2": null,
        "_X3": null,
        "_Y1": "X",
        "_Y2": null,
        "_Y3": null,
        "_Z1": null,
        "_Z2": null,
        "_Z3": "O",
        "_winner": null,
        "_winningCombination": null
    },
    "_winner": null,
    "_lastMove": "A3X2"
}

Edit: This code worked.

if (importPath) {
    const jsonData = fs.readFileSync(importPath);
    Object.assign(board, JSON.parse(jsonData));
    console.log(board);
}

Upvotes: 0

Views: 341

Answers (1)

jfriend00
jfriend00

Reputation: 708116

fs.readFileSync() does not accept a callback so your callback function is completely ignored. Instead, it returns the data directly from the function call as a Buffer or a String.

const data = fs.readFileSync(...)

If it has an error, it will throw an exception.

See the doc for details.

Upvotes: 3

Related Questions