czy ktoś wie dlaczego zwraca pustą tablice?
import { readFile, writeFile, readdir } from "fs/promises";
import { v4 } from "uuid";
import { Document } from "@interface/document.js";
class Store {
$collection: Document[] = [];
collection = async (): Promise<any> => {
let dir = await readdir("docs");
dir.forEach(async (file) => {
let document = await this.readDoc(file);
this.$collection.push(document); // <-- nic nie dodaje!
});
return this.$collection;
};
readDoc = async (uuid: string): Promise<Document> => {
let buffer = await readFile(`docs/${uuid}`, "utf-8");
let data: {} = await JSON.parse(buffer);
return { uuid, data };
};
writeDoc = async (data: {}): Promise<Document> => {
let uuid: string = v4();
await writeFile(
`docs/${uuid}.json`,
JSON.stringify(data, null, 4),
"utf-8"
);
return { uuid, data };
};
}
const store = new Store();
console.log(await store.collection()); // returns [] instead of [{"version": "1.0","extension": "json","path": "docs"}]
store.collection().then((collection) => {
console.assert(collection.length > 0);
});