pouchdb-community / ember-pouch

PouchDB/CouchDB adapter for Ember Data
Apache License 2.0
281 stars 76 forks source link

findRecord intermittently fails with inconsistent document structure #266

Open backspace opened 11 months ago

backspace commented 11 months ago

I’m working on adding conflict detection during sync and I’m encountering a strange inconsistent bug. Sometimes the sync and subsequent scan for conflicts succeeds, sometimes I get errors like this:

TypeError: Cannot set properties of undefined (setting 'id')
    at fromRawDoc (webpack://__ember_auto_import__/./node_modules/.pnpm/relational-pouch@4.0.4_pouchdb-find@8.0.1/node_modules/relational-pouch/lib/index.js?:24:66)
    at _parseRelDocs (webpack://__ember_auto_import__/./node_modules/.pnpm/relational-pouch@4.0.4_pouchdb-find@8.0.1/node_modules/relational-pouch/lib/index.js?:29:720)
    at _parseAlldocs (webpack://__ember_auto_import__/./node_modules/.pnpm/relational-pouch@4.0.4_pouchdb-find@8.0.1/node_modules/relational-pouch/lib/index.js?:29:281)
    at _find (webpack://__ember_auto_import__/./node_modules/.pnpm/relational-pouch@4.0.4_pouchdb-find@8.0.1/node_modules/relational-pouch/lib/index.js?:28:94)
    at async Class._findRecord (http://localhost:4190/assets/vendor.js:91649:21)

Looking at the function in question, frowRawDoc, the problem is that pouchDoc.data is sometimes undefined, like this:

{
  "name": "A region version 100",
  "_id": "region_2_778B4E9D-8C63-7D27-B28E-CBA85B8F8F97",
  "_rev": "2-a526bf91fa35aaceedf48f58dca458ad"
}

When the error doesn’t happen (most of the time) pouchDoc is the expected structure:

{
  "data": {
    "name": "A region",
    "hours": null,
    "accessibility": null,
    "notes": null,
    "x": null,
    "y": null,
    "createdAt": "2023-12-21T23:54:45.357Z",
    "updatedAt": "2023-12-21T23:54:45.357Z",
    "parent": null,
    "children": [],
    "destinations": [],
    "waypoints": []
  },
  "_id": "region_2_03F56E18-9ABB-B9AC-A0FB-8794D982CD1A",
  "_rev": "1-57da2cebd4d756df32f6b4205dbd577d"
}

The code for finding conflicts is here, it uses allDocs (with include_docs) but you can see in the commented-out code that I tried watching changes too, which actually seemed to produce more errors. Is this how people are working with conflicts, is there any way we can make them first-class with Ember Pouch models?

The actual failures are happening in relational-pouch but I’m not sure which library is actually at fault.