Node.js nodejs如何在方法之外获取fs.stat birthdate值
我想读取文件创建日期,并想将该日期推送到一个数组并发送回。当我在fs.stat方法中调用它时,它会显示日期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('./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
是函数范围内的局部变量。您可以尝试将其移出函数,但由于异步回调,它可能没有预期的值。