Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 使用fs.writeFile时未定义的值_Node.js_Fs - Fatal编程技术网

Node.js 使用fs.writeFile时未定义的值

Node.js 使用fs.writeFile时未定义的值,node.js,fs,Node.js,Fs,我正在使用node.js,我试图读取一个文件夹并返回file.length,然后我想将该值写入一个txt文件。问题是,当我尝试使用fs.writeFile时,它会创建一个只包含未定义的世界的文件 var length; fs.readdir(dir, function(err, file) { length = file.length+1; }); setTimeout(function(){ }, 1000); fs.writeFile('/public/images/value.t

我正在使用node.js,我试图读取一个文件夹并返回file.length,然后我想将该值写入一个txt文件。问题是,当我尝试使用fs.writeFile时,它会创建一个只包含未定义的世界的文件

var length;
fs.readdir(dir, function(err, file) {
   length = file.length+1;
});
setTimeout(function(){
}, 1000);  
fs.writeFile('/public/images/value.txt', length, function(err) {
     if(err) {
        return console.log(err);
       }

     console.log("The file was saved!");
     console.log(length);
});
log(长度)返回6


这是处理异步代码时的典型错误。之所以没有定义,是因为在执行fs.writefile函数时,第一个函数还没有完成对目录的读取,所以此时长度仍然没有定义。尝试将writefile函数放入setTimeout函数中。这应该是一个典型的错误,当你处理异步代码时。之所以没有定义,是因为在执行fs.writefile函数时,第一个函数还没有完成对目录的读取,所以此时长度仍然没有定义。尝试将writefile函数放入setTimeout函数中。这就足够了。

您在处理异步代码时遇到了一些错误。
fs.readir()
fs.writeFile()
都是异步的。这意味着,您知道它们完成的唯一方式是在回调中

fs.readdir(dir, function(err, files) {
    if (err) return console.log(err);
    let length = files.length + 1;
    fs.writeFile('/public/images/value.txt', length, function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
        console.log(length);
    });
});

// fs.readdir() has not yet completed when this part of your code runs
// it will complete some time late and then call its callback
// similarly fs.writeFile() has not yet completed when this part of your code runs
因此,如果要使用
fs.readdir()
的结果,那么应该在回调中使用它。如果您想知道何时完成了
fs.writeFile()
,并指望它的存在和更新,您只能在它的回调中完成

fs.readdir(dir, function(err, files) {
    if (err) return console.log(err);
    let length = files.length + 1;
    fs.writeFile('/public/images/value.txt', length, function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
        console.log(length);
    });
});

// fs.readdir() has not yet completed when this part of your code runs
// it will complete some time late and then call its callback
// similarly fs.writeFile() has not yet completed when this part of your code runs
此外,
setTimeout()
没有阻塞。它所做的只是安排一个计时器在将来某个时候启动,而您的其余代码将继续运行。因此,如果要在此处使用
setTimeout()
,则必须将
fs.writeFile()
代码放入
setTimeout()
回调中。但是,这是一种黑客行为,不是处理异步代码的推荐方法。您应该将
fs.writeFile()
放在
fs.readdir()
回调中,这样事情就100%可靠了


异步结果必须在表示异步操作完成的实际回调中使用,或者在从该回调中调用的函数中使用。

在处理异步代码时,您遇到了一些错误。
fs.readir()
fs.writeFile()
都是异步的。这意味着,您知道它们完成的唯一方式是在回调中

fs.readdir(dir, function(err, files) {
    if (err) return console.log(err);
    let length = files.length + 1;
    fs.writeFile('/public/images/value.txt', length, function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
        console.log(length);
    });
});

// fs.readdir() has not yet completed when this part of your code runs
// it will complete some time late and then call its callback
// similarly fs.writeFile() has not yet completed when this part of your code runs
因此,如果要使用
fs.readdir()
的结果,那么应该在回调中使用它。如果您想知道何时完成了
fs.writeFile()
,并指望它的存在和更新,您只能在它的回调中完成

fs.readdir(dir, function(err, files) {
    if (err) return console.log(err);
    let length = files.length + 1;
    fs.writeFile('/public/images/value.txt', length, function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
        console.log(length);
    });
});

// fs.readdir() has not yet completed when this part of your code runs
// it will complete some time late and then call its callback
// similarly fs.writeFile() has not yet completed when this part of your code runs
此外,
setTimeout()
没有阻塞。它所做的只是安排一个计时器在将来某个时候启动,而您的其余代码将继续运行。因此,如果要在此处使用
setTimeout()
,则必须将
fs.writeFile()
代码放入
setTimeout()
回调中。但是,这是一种黑客行为,不是处理异步代码的推荐方法。您应该将
fs.writeFile()
放在
fs.readdir()
回调中,这样事情就100%可靠了


异步结果必须在表示异步操作完成的实际回调内部使用,或在从该回调调用的函数中使用。

readdir
writeFile
是异步函数。您需要将
fs.writeFile
放入readdir的回调中,或者使用承诺来解析。问题是在
readdir
完成之前调用了
writeFile
,不,setTimeout不是停止该操作的好方法。
readdir
writeFile
是异步函数。您需要将
fs.writeFile
放入readdir的回调中,或者使用承诺来解析。问题是在
readdir
完成之前调用了
writeFile
,不,setTimeout不是一个很好的停止方法。我尝试了它,但它不起作用。我还试图删除fs.readdir函数,并将其替换为var length=6;只是为了测试它,但它与undefined有相同的问题。您是否处于“use strict”模式?无论我是否使用“use strict”,它都不起作用。这就是为什么我说超时是个坏主意,它不是等待代码执行的异步运行。我尝试了它,但它不起作用。我还试图删除fs.readdir函数,并将其替换为var length=6;只是为了测试它,但它与undefined有相同的问题。您是否处于“use strict”模式?无论我是否使用“use strict”,它都不起作用。这就是为什么我说超时是个坏主意,它不是等待代码运行的异步运行execute@george.angelop-这回答了你的问题吗?@george.angelop-这回答了你的问题吗?
fs.readdir(dir, function(err, files) {
    if (err) return console.log(err);
    let length = files.length + 1;
    fs.writeFile('/public/images/value.txt', length, function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
        console.log(length);
    });
});

// fs.readdir() has not yet completed when this part of your code runs
// it will complete some time late and then call its callback
// similarly fs.writeFile() has not yet completed when this part of your code runs