[Realm React Native] Arrays Don't Save in Realm Database Documents

Hello all, I’m having an issue where arrays refuse to save in my Realm database documents. I am using Realm with React Native, TypeScript.

Consider this case:

In models/index.ts:

import { Task, Vote } from './tasks';

// Create schema with ALL types, including types for embedded objects
const ALL_SCHEMA = [
  Task,
  Vote
]

export const TaskRealmContext = createRealmContext({
  schema: ALL_SCHEMA
});

In models/task.ts:

import { Realm } from '@realm/react';

// Embedded object I intend to put in arrays
export class Vote extends Realm.Object<Vote> {
  static embedded: boolean = true;
  userId?: string;
  priority: number;
  createdAt?: Date = new Date();
}

// Object intended to be Document
export class Task extends Realm.Object<Task> {
  _id: Realm.BSON.UUID = new Realm.BSON.UUID();
  body!: string;
  createdAt: Date = new Date();
  userId?: string
  editedAt?: Date;
  votes?: Vote[];
  test?: string[];

  static primaryKey = '_id';

  constructor(realm: Realm, {
    _id,
    body, 
    userId,
    
  }: any) {
    const newTask: any =  {
      _id,
      userId,
      body,
    }
    super(realm, newTask);
  }
}

In some file where I create a Task:

realm.write(() => {
  const testTask: any= {
    body: 'This is a test',
  }
  // This successfully creates a new Task
  const newTask: Task = realm.create('Task', testTask);

  // This works and correctly edits the task
  newTask.body = 'This really is a test'

  // These following arrays do not result in anything
  newTask.votes = [] 

  // At first I thought the issue was an array of objects, 
  // but even arrays of strings do not get saved
  newTask.test = ['This', 'doesnt', 'do', 'anything']
})

If I console.log(newTask) at the very end of the realm.write operation, I see the following result:

{
  "_id": "some_random_uuid",
  "body": "This really is a test",
  "votes": [],
  "test": ['This', 'doesnt', 'do', 'anything']
}

However, if I query Realm for the same Task and console.log it, I see the following:

{
  "_id": "some_random_uuid",
  "body": "This really is a test",
}

In other words, it appears there is a silent failure in which all of the object manipulations appear to work within the realm.write operation, but once the object finally gets written to the database, the arrays do not get saved in the resultant document.

I’ve tried a few things:

  • Adding Array<Vote> to the schema leads to an error: Exception in HostFunction: Failed to read Realm object constructor must of a 'schema property.'
  • Adding Vote[] to the schema raises a warning: An element access expression should take an argument and crashes the app.

I can get embedded objects to work, but for my current use case I desperately need an array. But my arrays are just refusing to get saved into my Realm database documents.

Thus far, I have:

  • Added Embedded Objects to my Realm schema—though this seems to not be the problem, as even arrays of “basic” data types (such as strings) do not get stored in the end
  • Declared schemas for documents that include arrays (thingy[]), but, again, this doesn’t seem to have any impact.

Am I missing anything?

Help!

Okay so I have somewhat of a solution:

In the schema, instead of:

votes?: Vote[];

I have:

votes: Realm.List<Vote>

The issues here are:

  • The parameters are no longer nullable; tasks must always point to an array
  • TypeScript is furious with me:
Type 'undefined[]' is missing the following properties from type 
'List<MessageReaction>': type, optional, toJSON, description, and 12 more.

But it works. It freakin works. So I’m not gonna complain and will take the red TypeScript underlines in stride.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.