Docs Menu

Client Resets - Node SDK

On this page

  • Overview
  • Example

When using Realm Sync, a client reset is a serious error recovery task that your client app must perform in the following situation:

  • The given synced realm on the server was restored from a backup.
  • The client app made changes to that realm since the backup was made, but did not sync those changes back to the server before the server was restored.

In other words, the client app must carry out a client reset on a given synced realm if the server is restored to a version older than the version on the client.

Warning

A client reset erases all local data and downloads a new copy of the data stored in MongoDB Atlas. Performing a client reset loses all local changes made since the client last successfully synced.

Define an error handler function in your SyncConfiguration object.

Initiate a client reset if the error.name is "ClientReset". Call Realm.App.Sync.initiateClientReset() with the realm App and the realm path.

The error object also contains the location of the backup copy of the Realm file once the client reset process is carried out via the error.config.path. You can optionally move this backup file, using the fs.renameSync method, to a known location for a restore.

Discard the reference to the realm instance by setting the realm to null.

let realm = await Realm.open(config);
function errorSync(_session, error) {
if (realm) {
if (error.name === "ClientReset") {
const realmPath = "<Your Realm Path>";
realm.close();
console.log(`Error ${error.message}, need to reset ${realmPath}…`);
Realm.App.Sync.initiateClientReset(app, realmPath); // pass your realm app instance, and realm path to initiateClientReset()
console.log(`Creating backup from ${error.config.path}…`);
// Move backup file to a known location for a restore
fs.renameSync(error.config.path, realmPath + "~");
// Discard the reference to the realm instance
realm = null;
} else {
console.log(`Received error ${error.message}`);
}
}
}
var config = {
schema: [DogSchema], // predefined schema
sync: {
user: app.currentUser,
partitionValue: "MyPartitionValue",
error: errorSync,
},
};
Note

For an example of client reset within an app, see the realm-practice repo on GitHub.

On this page

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.