W
W
WIN_322020-05-31 20:49:09
JavaScript
WIN_32, 2020-05-31 20:49:09

How to write data to object from fs function in javascript?

There is such a code. I want to pass data from fs.stat and write it to a variable, but I don't quite understand how to do it. If you just output the entire object to the console, then the information is displayed, but if you refer to a specific element, it is empty. Tell me how you can display data from fs.stat? And you can do something like return data?

var folders2 = {
    items: []
}
fs.stat('D:\\MEGA\\Завершенные скрипты\\AppInstaller\\apps\\Архиваторы', function (err, stats) {
    if (stats.isFile()) {
        type = 'file'
    }

    if (stats.isDirectory()) {
        type = 'directory'
    }
    
    folders2.items[0] = {
        ext: type,
        type: type
    }
    folders2.items[1] = {
        ext: type,
        type: type
    }
});
console.log(folders2.items) // а тут есть
console.log(folders2.items[0]) // пусто
console.log(folders2.items[0].type) // пусто


5ed3ebbc09ca9389711855.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sh84, 2020-05-31
@WIN_32

Apparently it makes no sense to talk about asynchrony.
Just fs.statSync

K
Kristal1, 2020-05-31
@Kristal1

it's impossible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question