Я создаю базу данных noSQL для приложения NodeJS.Я хочу, чтобы база данных существовала как собственный класс.Однако после перемещения кода инициализации моей базы данных внутри модуля он больше не работает.LokiJS не может загрузить базу данных, и я не могу создать коллекции.Результат загрузки базы данных равен нулю, this.db не определен, и попытка получить коллекцию приводит к Uncaught TypeError: Cannot read property 'getCollection' of undefined
.
module.exports = Database;
function Database() {
this.db;
this.videos;
this.playlists;
this.init = function () {
const loki = require("lokijs");
const lfsa = require('../../node_modules/lokijs/src/loki-fs-structured-adapter.js');
var adapter = new lfsa();
this.db = new loki(`${localPath}db.json`, { adapter: adapter, autoload: true, autosave: true, autosaveInterval: 4000 });
this.db.loadDatabase({}, function (result) {
console.log(result); // This is null
alert(this.db); // This is undefined
alert(this.db.getCollection("SampleCollection")); // Uncaught TypeError: Cannot read property 'getCollection' of undefined
});
}
}