0

I want get the every data on the folder to nodeJS my code can read the data but can't assign the data to object I want just read and can assign the code even don't use the object

can you help me?

I'm not good well english so if you point out the my mistake I will very thankful

The code:

var fs = require('fs');

//get the every source of the file that in the data folder 
fs.readdir(`data`, function(error, file) {
  var newObject = {};

  file.forEach(function (element) {
    newObject[element]=0;
  });

  for(var key in newObject) {
    fs.readFile(`data/${key}`,'utf8', function(err, des){
      newObject[`${key}`]=`${des}`;
      console.log(`${des}`);
    });

  }

  console.log(newObject);

});
  • It's unclear what the problem is here. Are there errors? You never try to *read* data from the object. How do you know it doesn't work? – Quentin Aug 03 '20 at 08:31
  • I want read the many file to certain name so I use the object but it's object does't get the value – KR_GM_present Aug 03 '20 at 08:40
  • 1
    You're logging the value before its been assigned. Duplicate: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Aug 03 '20 at 08:44

0 Answers0