Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 33a26b0

Browse files
authored
Merge pull request #212 from TriliumNext/bugfix/sync_failure
Sync failed: Cannot read properties of undefined (reading 'utcDateChanged')
2 parents ab23459 + ea47668 commit 33a26b0

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"start-server-no-dir": "cross-env TRILIUM_SAFE_MODE=1 TRILIUM_ENV=dev TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 nodemon src/www.ts",
1818
"qstart-server": "npm run qswitch-server && TRILIUM_SAFE_MODE=1 TRILIUM_DATA_DIR=./data TRILIUM_ENV=dev TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 nodemon src/www.ts",
1919
"start-electron": "rimraf ./dist && tsc && ts-node ./bin/copy-dist.ts && cross-env TRILIUM_SAFE_MODE=1 TRILIUM_DATA_DIR=./data TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 TRILIUM_ENV=dev electron ./dist/electron.js --inspect=5858 .",
20-
"start-electron-no-dir": "cross-env TRILIUM_SAFE_MODE=1 TRILIUM_ENV=dev TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 electron --inspect=5858 .",
20+
"start-electron-no-dir": "rimraf ./dist && tsc && ts-node ./bin/copy-dist.ts && cross-env TRILIUM_SAFE_MODE=1 TRILIUM_ENV=dev electron ./dist/electron.js --inspect=5858 .",
2121
"qstart-electron": "npm run qswitch-electron && TRILIUM_SAFE_MODE=1 TRILIUM_DATA_DIR=./data TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 TRILIUM_ENV=dev electron --inspect=5858 .",
2222
"start-test-server": "npm run qswitch-server; rm -rf ./data-test; cross-env TRILIUM_SAFE_MODE=1 TRILIUM_DATA_DIR=./data-test TRILIUM_SYNC_SERVER_HOST=http://tsyncserver:4000 TRILIUM_ENV=dev TRILIUM_PORT=9999 ts-node src/www.ts",
2323
"switch-server": "rm -rf ./node_modules/better-sqlite3 && npm install",
24-
"switch-electron": "./node_modules/.bin/electron-rebuild",
24+
"switch-electron": "electron-rebuild",
2525
"qswitch-server": "rm -rf ./node_modules/better-sqlite3/bin ; mkdir -p ./node_modules/better-sqlite3/build ; cp ./bin/better-sqlite3/linux-server-better_sqlite3.node ./node_modules/better-sqlite3/build/better_sqlite3.node",
2626
"qswitch-electron": "rm -rf ./node_modules/better-sqlite3/bin ; mkdir -p ./node_modules/better-sqlite3/build ; cp ./bin/better-sqlite3/linux-desktop-better_sqlite3.node ./node_modules/better-sqlite3/build/better_sqlite3.node",
2727
"build-backend-docs": "rm -rf ./docs/backend_api && ./node_modules/.bin/jsdoc -c jsdoc-conf.json -d ./docs/backend_api src/becca/entities/*.js src/services/backend_script_api.js src/services/sql.js",

src/services/sync_update.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ function updateEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, instan
7575
}
7676

7777
function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, instanceId: string, updateContext: UpdateContext) {
78-
const localEC = sql.getRow<EntityChange>(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);
78+
const localEC = sql.getRow<EntityChange | undefined>(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);
79+
const localECIsOlderOrSameAsRemote = (
80+
localEC && localEC.utcDateChanged && remoteEC.utcDateChanged &&
81+
localEC.utcDateChanged <= remoteEC.utcDateChanged);
7982

80-
if (!localEC.utcDateChanged || !remoteEC.utcDateChanged) {
81-
throw new Error("Missing date changed.");
82-
}
83-
84-
if (!localEC || localEC.utcDateChanged <= remoteEC.utcDateChanged) {
83+
if (!localEC || localECIsOlderOrSameAsRemote) {
8584
if (remoteEC.isErased) {
8685
if (localEC?.isErased) {
8786
eraseEntity(remoteEC); // make sure it's erased anyway
@@ -104,7 +103,7 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow,
104103
}
105104

106105
if (!localEC
107-
|| localEC.utcDateChanged < remoteEC.utcDateChanged
106+
|| localECIsOlderOrSameAsRemote
108107
|| localEC.hash !== remoteEC.hash
109108
|| localEC.isErased !== remoteEC.isErased
110109
) {
@@ -113,7 +112,7 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow,
113112

114113
return true;
115114
} else if ((localEC.hash !== remoteEC.hash || localEC.isErased !== remoteEC.isErased)
116-
&& localEC.utcDateChanged > remoteEC.utcDateChanged) {
115+
&& !localECIsOlderOrSameAsRemote) {
117116
// the change on our side is newer than on the other side, so the other side should update
118117
entityChangesService.putEntityChangeForOtherInstances(localEC);
119118

0 commit comments

Comments
 (0)