techfort / LokiJS

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

TypeError: Cannot add property updated, object is not extensible #826

Closed kahanu closed 4 years ago

kahanu commented 4 years ago

I'm having a problem updating objects in the collection. In my Angular 8 application I've built a LokiService that has all the basic CRUD operations, and so far Update is the one giving me grief. I've looked everywhere for a reliable solution and I haven't found one.

Here is the put method of my service:

  put(data: T): Observable<T> {
    const coll = this.db.getCollection(this.collName);
    console.log('data to be updated: ', data);

    let found = coll.findObject({ id: data.id });
    console.log('found: ', found);

    const foundUpdated = {...found, ...data};
    console.log('found updated: ', foundUpdated);
    coll.update(foundUpdated);

    return of(foundUpdated);
  }

This is the output of the console.log() and the error that appears:

data to be updated: {id: 4, name: "Security Protocols"}

found: {id: 4, name: "Security Protocols Group", meta: {…}, $loki: 8}

found updated: {id: 4, name: "Security Protocols", meta: {…}, $loki: 8}

Everything is correct up to this point, as far as I know, then the Chrome tools displays this error and the update never happens.

Note: for the argument of the put method (data to be updated), I can actually return the full LokiJs object with the updated value that includes the $loki and meta properties, but I get the same error as below.

core.js:4002 
ERROR TypeError: Cannot add property updated, object is not extensible
    at Collection.updateMeta (lokijs.js:5090)
    at Collection.update (lokijs.js:5913)
    at LokiPolicyGroupService.push../src/app/_lib/modules/loki/loki.service.ts.LokiServiceBase.put (loki.service.ts:62)
    at MergeMapSubscriber.project (group.effects.ts:40)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:61)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:51)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:53)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:53)
    at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)

I looked at the issue here, https://github.com/techfort/LokiJS/issues/420 and it has an example, that works in the sandbox, but not in Angular.

I tried applying that function, but I get the error that says:

TypeError: Cannot assign to read only property 'id' of object '[object Object]'

Any insight on what I'm doing wrong is appreciated. So at the moment I can do everything except Update.

Bandito11 commented 4 years ago

It should work as written in your example, so weird.

When you use const coll = this.db.getCollection(this.collName); have you tried to see what value that variable contains on the console.log? Sometimes errors in update is because the getCollection is null because the dbis not initialized as it hasn't loaded the database (async problems). This happen in Angular do to how Classes work with .this.

stale[bot] commented 4 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.