techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.73k stars 482 forks source link

Cannot read property 'chain' of null, Electron #872

Closed GobsRuiz closed 2 years ago

GobsRuiz commented 3 years ago

I'm trying to return a list / array of database data.

I found some examples, a little confusing, I tried here but it didn't work. My code right now looks like this

var clientes = db.getCollection('clientes'); var resultado = clientes.chain().simplesort("mame").data(); console.log(resultado)

In the console it returns this: "index.js:9 Uncaught TypeError: Cannot read property 'chain' of null"

i'm using loki js

work67 commented 3 years ago

What do you get if you try: var items = clientes.data

explanation: "data" can be two things:

  1. a property of the collection - gets you all collection data as an array of objects. Careful because it is passed by reference, so for example if you modify "items" above like: delete items[0] the 1st document from the collection clientes will be deleted.

  2. "data" can also mean the data() method (function) of the resultSet object - it does a similar job as "data" property above but is used with chain()

I suppose you are not initialising the "db" properly so whatever you do will return null/undefined. That's why I asked about what clientes.data returns.

Cheers.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.