Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Javascript 为什么;“错误的论点”;fs.write中出错_Javascript_Node.js - Fatal编程技术网

Javascript 为什么;“错误的论点”;fs.write中出错

Javascript 为什么;“错误的论点”;fs.write中出错,javascript,node.js,Javascript,Node.js,嗯。 所以我试图从一个文件中读取一个数字,它代表一个计数器,做一些循环,每次写入第二个文件,然后在我想的时候停止,并将计数器的新值写入同一个第一个文件 var textPath = "/kolodvori.txt"; var countPath = "/dijestao.txt"; var buffer = new Buffer(0); fs.readFile(countPath, function (err,data){ if (err) console.log(err);

嗯。 所以我试图从一个文件中读取一个数字,它代表一个计数器,做一些循环,每次写入第二个文件,然后在我想的时候停止,并将计数器的新值写入同一个第一个文件

var textPath = "/kolodvori.txt";
var countPath = "/dijestao.txt";
var buffer = new Buffer(0);

fs.readFile(countPath, function (err,data){
    if (err) console.log(err);
    else {      
        console.log(data);
        i = data;

        var stream2 = fs.createWriteStream(textPath, {flags: 'a'});

        stream2.once('open', function (fd){ 
            fs.open(countPath,'w', function (fd2){
                getPageHtml(defHost, firstNewPath, i, function (html,count,callback){

                    if (count%10==0) console.log(count);

                    ++count;
                    i = new Buffer(count.toString());

                    //console.log('i:' + i);
                    fs.write(fd2, i, 0, i.length, null, function (err,len,buff){ console.log(err); });

                    var newPath = defPath.replace(/xxxKOLO1xxx/gi, count);

                    getPageHtml(defHost, newPath, count, callback);
                });
            });
        });
    }
});
经过我所有的努力,我得到了一份工作

fs.js:513
binding.write(fd, buffer, offset, length, position, wrapper);
        ^
TypeError: Bad argument
     at Object.fs.write (fs.js:513:11)
在这种情况下,“坏论点”到底意味着什么? 这与我有两种读取和写入同一文件的方式有关吗?

改用
path.join(
)和
path.join(

使用
/
意味着相对于
根目录,并且您希望相对于脚本的目录


记住做
var path=require(“path”)在顶部。

我也有同样的错误,最后使用了writeFile


谢谢你。这是我必须解决的第二件事。不幸的是,这与当前的问题无关。你现在得到了什么?同样的。问题不在文件的位置您可能有错误的回调函数?将
function(fd2)
更改为
function(err,fd2)
不,在修复后工作相同:-)文档如下:回调获取两个参数
(err,fd)
。祝你好运,昆吉。对不起,伙计,是的,这解决了问题