在某些文件夹中无法执行fs.write操作

时间:2019-05-01 09:20:53

标签: javascript node.js fs

我正在尝试使用json文件作为存储系统来创建类似数据库的结构,并且我在功利类中创建了一个方法来帮助解决此问题,但是根据所在位置的路径,我无法使用它我想写在。

我已经确定该方法有效,但是仅在某些路径中,我知道该路径每次都是正确的,但是某些路径只会使程序挂起;没有错误,甚至没有留下我留下的调试日志。

这里是将对象保存在选定路径中的方法。

    /*
    ** Saves `file` object into `path`.
    ** Will attempt backup first, and may reload a backup on errors
    */
    save: function(path, file) {
        this.backup(path, file, () => {
            FS.writeFile(path, JSON.stringify(file), (err) => {
                if (err) {
                    this.log(err, this.LogTypes.ERROR);
                    this.log(`Something wrong happened, FS.writeFile operation on '${path}' failed!`, this.LogTypes.ERROR);
                    if (FS.existsSync(`${path}.bak`)) {
                        this.log(`Reverting '${path}' to backup!`, this.LogTypes.DATABASE);
                        FS.copyFile(`${path}.bak`, `${path}`, (err) => {
                            if (err) {
                                this.log(err, this.LogTypes.ERROR);
                                this.log(`Unable to restore backup from '${path}.bak'! '${path} may have been corrupted or destroyed!'`, this.LogTypes.ERROR);
                            } else this.log(`'${path}' has been restored to the latest backup successfully.`, this.LogTypes.DATABASE);
                        });
                    } else this.log(`Backup for '${path}' was not found, cannot restore backup!\nPlease make sure that ${path} is not damaged, or is an operation-critical file, and restart the bot.`, this.LogTypes.ERROR);
                } else {
                    this.log(`'${path}' has been saved successfully!`, this.LogTypes.DATABASE);
                }
            });
        });
    }

它在代码中的实现是通过使用Utils.save("path"; Object)

0 个答案:

没有答案