This code is for an Ionic app written in typescript:
let fileNames: any[] = [];
fileNames = this.getFileNames("wildlife");
console.log("file names:", fileNames);
this.displayFiles(fileNames);
The output shows a strange result, as even though there are 50 items present, it displays Array(0). The issue arises when passing the array to a function:
displayFiles( files)
{
files.forEach(item => {
console.log(item) <- item does not exist
});
}
The array turns out to be empty. Why is that happening? Is there another way to pass the array and iterate through its objects?
EDIT:
getFileNames(folder) {
let fileNames: any[] = [];
this.file.listDir(this.file.applicationDirectory, 'www/assets/' + folder)
.then((items) => {
// console.log(items);
items.forEach(item => {
fileNames.push({
fileName: item.name,
name: this.getName( item.name)
});
});
})
.catch(err =>
console.log("error: ", err));
return fileNames;
}