Skip to content

TypeError: Cannot read property 'collections' of null #906

@chuongle

Description

@chuongle

Today I encountered a weird issue with LokiJS. For whatever reason, the main db file (metadata) got wiped, the file is still there but the content is empty. So when the application started again, I got the TypeError: Cannot read property 'collections' of null error. The issue is I can catch any of these error. The only reason I am aware of this error is because I have sentry setup and it automatically caught it.

Here is the code I use:

try {
  const db = new Loki('test.db', {
    adapter: new LokiFsStructuredAdapter(),
    autosave: true,
    autosaveInterval: 5000
  });
  
  db.loadDatabase({}, (error) => {
    if (error) {
      console.log('fail to load error: ', error);
    } else {
      console.log('successfully load db')
      // set up collections and insert data
    }
  });  
} catch (error)
  console.log('error', error);
}

Here is the full error:

TypeError: Cannot read property 'collections' of null
    at Interface.eval (webpack:///./node_modules/lokijs/src/loki-fs-structured-adapter.js?:153:31)
    at Interface.emit (events.js:327:22)
    at Interface.EventEmitter.emit (domain.js:467:12)
    at Interface.close (readline.js:424:8)
    at ReadStream.onend (readline.js:202:10)
    at ReadStream.emit (events.js:327:22)
    at ReadStream.EventEmitter.emit (domain.js:467:12)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

I am not sure what is going on here. Is there a right way of handling error that I am missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions