Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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 nodejs如何在方法之外获取fs.stat birthdate值_Node.js_Fs - Fatal编程技术网

Node.js nodejs如何在方法之外获取fs.stat birthdate值

Node.js nodejs如何在方法之外获取fs.stat birthdate值,node.js,fs,Node.js,Fs,我想读取文件创建日期,并想将该日期推送到一个数组并发送回。当我在fs.stat方法中调用它时,它会显示日期 fs.stat('./templates/'+file , function(err, stats){ var createdDate = stats.birthtime; console.log(createdDate , showing date) }) 但当我试图称之为该方法的过时时,它显示出未定义 fs.stat('

我想读取文件创建日期,并想将该日期推送到一个数组并发送回。当我在fs.stat方法中调用它时,它会显示日期

      fs.stat('./templates/'+file , function(err, stats){
      var createdDate = stats.birthtime;
      console.log(createdDate , showing date)
        })
但当我试图称之为该方法的过时时,它显示出未定义

      fs.stat('./templates/'+file , function(err, stats){
      var createdDate = stats.birthtime;
        })
       console.log(createdDate , undefined)

您需要使用
var stats=fs.statSync('./templates/'+文件)取而代之。

根据-此操作在没有回调的情况下同步执行。

您需要使用
var stats=fs.statSync('./templates/'+文件)取而代之。

根据-此操作在没有回调的情况下同步执行。

createDate
是函数范围内的局部变量。您可以尝试将其移出函数,但由于异步回调,它可能没有预期的值。
createDate
是函数范围内的局部变量。您可以尝试将其移出函数,但由于异步回调,它可能没有预期的值。