Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 尝试包装缓冲写入程序时发生异常_Node.js - Fatal编程技术网

Node.js 尝试包装缓冲写入程序时发生异常

Node.js 尝试包装缓冲写入程序时发生异常,node.js,Node.js,免责声明:我对node(但不是JavaScript)相当陌生 我只是想写一个logger类,它在内存中一次保存几行,然后在缓冲区达到容量时刷新到磁盘 问题 调用我的记录器包装器会导致缓冲写入程序中出现异常 我肯定我误解了require()的工作原理,还有很多人建议我使用新的chatlogs.Chatlogger()创建对象,但是我没有看到其他很多节点库使用这种工作方式 /www/im/node_modules/buffered-writer/lib/buffered-writer.js:125

免责声明:我对node(但不是JavaScript)相当陌生

我只是想写一个logger类,它在内存中一次保存几行,然后在缓冲区达到容量时刷新到磁盘


问题 调用我的记录器包装器会导致缓冲写入程序中出现异常

我肯定我误解了require()的工作原理,还有很多人建议我使用新的chatlogs.Chatlogger()创建对象,但是我没有看到其他很多节点库使用这种工作方式

/www/im/node_modules/buffered-writer/lib/buffered-writer.js:125
    cb ();
        ^
TypeError: undefined is not a function
    at Writer.flush (/www/nodeim/node_modules/buffered-writer/lib/buffered-writer.js:125:3)
    at Chatlogger.close (/www/nodeim/helpers/chatlogs.js:27:14)
    at Object.<anonymous> (/www/nodeim/app.js:76:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

包装类

./helpers/chatlogs.js
我是这个模块的作者。您需要将回调传递给flush函数,但不需要调用flush。当缓冲写入程序关闭或写入时超过缓冲区大小时,数据将自动刷新到磁盘


太棒了,谢谢你的帮助。我想我应该在flush()上使用RTFM,但感觉问题更多地出现在我的包装器类中,而不是我调用缓冲编写器的方式
var chatlogs = require('./helpers/chatlogs.js'); 
var chatlogger_obj = new chatlogs.Chatlogger();

chatlogger_obj.open("logs/log.txt");
chatlogger_obj.log("TESTING");
chatlogger_obj.close();

process.exit(0);
exports.version = '0.0.1';

var 
  buffer = require('buffered-writer'),
    fs = require('fs');

var Chatlogger = function() {
    this.handle = null,
    this.filename = "",
    this.dirtyops = 0;
}

Chatlogger.prototype.open = function (filename) {

    //fs.unlink(filename);

    this.filename = filename;
    this.handle = buffer.open(filename)
        .on ("error", function (error) {
            //this.handle = null;
            console.log (error);
        });
}

Chatlogger.prototype.close = function() {
    console.log("CLOSING");
    this.handle.flush();
    this.handle.close();
    this.handle = null;
}

Chatlogger.prototype.log = function (str) {

    console.log(str);
    this.handle.writeln(str);

    if (this.dirtyops++ > 5)
    {
        console.log("FLUSHING");
        this.handle.flush();
        this.dirtyops = 0;
    }
}

module.exports.Chatlogger = Chatlogger;