Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2847

Data Loss in Nested Objects When Using Spread Operator for Update in Realm JS

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintableJSON

      How frequently does the bug occur?

      Always

      Description

      I am experiencing data loss in nested objects when updating an object in Realm JS using the spread operator. Specifically, after the update, one of my nested lists (trackingDataList) gets reset to an empty list.

      Realm models

      class TrackingData extends Realm.Object {
      	id!: number;
      	progress!: number;
      
      	static schema: Realm.ObjectSchema = {
      		name: 'TrackingData',
      		primaryKey: 'id',
      		properties: {
      			id: 'int',
      			progress: 'int',
      		},
      	};
      }
      
      export default class Profile extends Realm.Object {
      	id!: string;
      	attemptID!: number;
      	lastAccessDate!: Date | null;
      	trackingDataList!: TrackingData[];
      	sortOrder!: number;
      	from!: Date;
      	userId!: string;
      
      	static schema: Realm.ObjectSchema = {
      		name: 'Profile',
      		primaryKey: 'id',
      
      		properties: {
      			id: 'string',
      			attemptID: 'int',
      			lastAccessDate: {type: 'date', optional: true},
      			trackingDataList: 'TrackingData[]',
      			sortOrder: 'int',
      			from: 'date',
      			userId: 'string',
      		},
      	};
      }
      

      Code Sample

      const updatedProfile = { ...currentProfile, lastAccessDate: Moment.utc().format() };
      
      realm().write(() => {
        realm().create('Profile', updatedProfile, 'modified');
      });
      

      Before Update:

      {
        "id": "12345_2_2",
        "attemptID": 2,
        "lastAccessDate": null,
        "trackingDataList": [
          {
            "id": 1,
            "progress": 50
          }
        ],
        "sortOrder": 1,
        "from": "2024-06-10T10:27:02.909Z",
        "userId": "12345"
      }
      

      After Update

      {
        "id": "12345_2_2",
        "attemptID": 2,
        "lastAccessDate": "2024-06-25T16:26:43Z",
        "trackingDataList": [],
        "sortOrder": 1,
        "from": "2024-06-10T10:27:02.909Z",
        "userId": "12345"
      }
      

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      N/A
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      1. Create an object in Realm with nested objects/lists.
      2. Update the object using the spread operator and change a single key.
      3. Observe that some nested lists are reset or missing after the update.

      Version

      12.10.0

      What services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      iOS 17.2

      Build environment

      react-native: 0.74.2
      node: v18.17.1

      Cocoapods version

      1.15.2

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Archiver:
            marc.greenfield@mongodb.com Marc Greenfield

              Created:
              Updated:
              Resolved:
              Archived: