question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when editing a same row twice

See original GitHub issue

I am using ServerDataSource:

  let data = event.newData;

  this.tableService.update(data)
     .then((result) => {
         event.confirm.resolve(result[0]);
      }).catch((err) => {
         event.confirm.reject(err);
      });

Error: Uncaught (in promise): Error: Element was not found in the dataset Error: Element was not found in the dataset at ServerDataSource.LocalDataSource.find (http://localhost:8080/vendor.bundle.js:842:31) [angular] at LocalDataSource.update (http://localhost:8080/vendor.bundle.js:831:19) [angular] at new ZoneAwarePromise (http://localhost:8080/polyfills.dll.js:4471:29) [angular] at ServerDataSource.LocalDataSource.update (http://localhost:8080/vendor.bundle.js:830:16) [angular] at http://localhost:8080/vendor.bundle.js:1578:26 [angular] at Object.onInvoke (http://localhost:8080/vendor.dll.js:30260:37) [angular] at Zone.run (http://localhost:8080/polyfills.dll.js:4033:43) [angular => angular] at http://localhost:8080/polyfills.dll.js:4455:57 [angular] at Object.onInvokeTask (http://localhost:8080/vendor.dll.js:30251:37) [angular] at ZoneDelegate.invokeTask (http://localhost:8080/polyfills.dll.js:4194:40) [angular] at Zone.runTask (http://localhost:8080/polyfills.dll.js:4071:47) [<root> => angular] at drainMicroTaskQueue (http://localhost:8080/polyfills.dll.js:4353:35) [<root>] at XMLHttpRequest.ZoneTask.invoke (http://localhost:8080/polyfills.dll.js:4269:25) [<root>] at resolvePromise (http://localhost:8080/polyfills.dll.js:4421:31) [angular] at resolvePromise (http://localhost:8080/polyfills.dll.js:4406:17) [angular] at http://localhost:8080/polyfills.dll.js:4455:17 [angular] at Object.onInvokeTask (http://localhost:8080/vendor.dll.js:30251:37) [angular] at ZoneDelegate.invokeTask (http://localhost:8080/polyfills.dll.js:4194:40) [angular] at Zone.runTask (http://localhost:8080/polyfills.dll.js:4071:47) [<root> => angular] at drainMicroTaskQueue (http://localhost:8080/polyfills.dll.js:4353:35) [<root>] at XMLHttpRequest.ZoneTask.invoke (http://localhost:8080/polyfills.dll.js:4269:25) [<root>] ------------- Elapsed: 14 ms; At: Fri Feb 24 2017 17:24:24 GMT-0300 (BRT) -------------
at getStacktraceWithUncaughtError (http://localhost:8080/polyfills.dll.js:3790:12) [angular] at new LongStackTrace (http://localhost:8080/polyfills.dll.js:3784:22) [angular] at Object.onScheduleTask (http://localhost:8080/polyfills.dll.js:3840:18) [angular]

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:9

github_iconTop GitHub Comments

10reactions
daddymancommented, Jun 28, 2017

The problem is that there is code that assumes the object instance will be the same before and after the update occurs.

In local.data-source.ts the find method compares the instances using ===.

find(element: any): Promise<any> {
    const found = this.data.find(el => el === element);
    if (found) {
      return Promise.resolve(found);
    }
   return Promise.reject(new Error('Element was not found in the dataset'));
}

In grid.ts the call to findRowByData compares using ===

this.source.onUpdated().subscribe((data) => {
    const changedRow = this.dataSet.findRowByData(data);
    changedRow.setData(data);  //fails in here because data is undefined
});

data-set.ts:

findRowByData(data: any): Row {
    return this.rows.find((row: Row) => row.getData() === data);
}

When using a server data source the elements change each time they are loaded. The first edit/update works because the object hasn’t change but then getElements is called on the data source and the elements are different (in some places). When the edit/update occurs there is a mismatch.

To get around this I added update and find methods to completely override LocalDataSource to match elements by the id field I have in my objects. When the object is updated I copy the fields from the new values object passed to the update method and send that object to the updated event.

My first attempt was to not extend LocalDataSource but I still had the problem of the issue with data-set.ts comparing reference instead of testing by an id field.

Here is what I added to my data source class - which extends from LocalDataSource. I can now edit/update multiple times.

public update(element: Trigger, values: Trigger): Promise<any> {
    return new Promise((resolve, reject) => {
        this.find(element).then(found => {
            //Copy the new values into element so we use the same instance
            //in the update call.
            element.name = values.name;
            element.enabled = values.enabled;
            element.condition = values.condition;

            //Don't call super because that will cause problems - instead copy what DataSource.ts does.
            ///super.update(found, values).then(resolve).catch(reject);
            this.emitOnUpdated(element);
            this.emitOnChanged('update');
            resolve();
        }).catch(reject);
    });
}
public find(element: Trigger): Promise<Trigger> {
    //Match by the trigger id
    const found: Trigger = this.data.find(el => el.id === element.id);
     if (found) {
        return Promise.resolve(found);
    }
    return Promise.reject(new Error('Element was not found in the dataset'));
}
2reactions
blushift80commented, Oct 10, 2019

I solved by invoking the refresh() method of the datasource after event.confirm.resolve() in onEditConfirm function: e.g .: onEditConfirm(ev){ this.httpapi.edit(ev.newData).subscribe( result => { if (result.status) { … ev.confirm.resolve(result.data); this.source.refresh(); } else …

this works fine for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MERGE statement attempted to UPDATE or DELETE the ...
The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than...
Read more >
Update same row twice – SQLServerCentral Forums
I am using this update to move the data from #stageTable to the TRValue table. Of course it brings in the first 3...
Read more >
Error: The MERGE statement attempted to UPDATE or ...
The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than...
Read more >
Unable to edit record twice - Power Platform Community
... works fine until I try to edit the same record again (twice in a row). ... save and I get an ETAG...
Read more >
EF4 - Updating the same table row twice in the same context ...
So, what are you trying to do? You've told EF and the database (I assume) that "id" is the unique identifier for an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found