0

I want to read the file created date and want to push that date to one array and send back. Its showing date when am calling it inside of fs.stat method

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

but when am trying to call it oustide of that method its showing undefined

      fs.stat('./templates/'+file , function(err, stats){
      var createdDate = stats.birthtime;
        })
       console.log(createdDate , undefined)
pradeep ks
  • 11
  • 2
  • 9
  • `createDate` is a local variable with the scope of the function. You can try to put it out of the function, but it might not have the expected value due to asynchronous callback. – Andre Albert Jan 25 '18 at 12:40

1 Answers1

2

You need to use var stats = fs.statSync('./templates/'+file); instead. According to having-trouble-understanding-how-fs-stat-works - this operation executed synchronous without a callback.

Andre Albert
  • 1,386
  • 8
  • 17