0
var jsonObj= [{}]

function read_directory(){
 foreach files in directory{
  //do stuff to file
  jsonObj.push(newdata) //newdata is also an json object
  console.log(jsonObj) //this returns the current object after each push
 }
 console.log(jsonObj) //this returns an empty json object
}

Why does the object appear to be empty when it is called outside of the loop. When it is called inside of the loop, the data is pushed correctly. But when I want to save the complete object after the loop it is empty.

Niels Hop
  • 11
  • 1
  • When is the `read_directory` function in relation to where the outer `console.log(jsonObj)` is called? – VLAZ Jun 23 '20 at 08:19
  • 6
    The file operations are probably asynchronous. Please add the actual code instead of pseudocode. – adiga Jun 23 '20 at 08:19
  • Also, most likely related [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) and [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) – VLAZ Jun 23 '20 at 08:20

0 Answers0