How to delete in Realm in Javascript

Asked

Viewed 441 times

0

I am trying to delete an item from a list using Realm, but all the methods I have tried so far have been failed.

The last one I tried for a few tips, it should work, but still no success.

View the code on github: https://github.com/andrelinos/react-native-realmdb/blob/master/src/pages/Main/index.js

I managed to erase the entire base using the code below:

realm.write(() => {
      realm.delete(realm.objects('Repository'));
    });

But deleting only one item, still unsuccessful. The code below should work, but simply returns error.

      realm.delete(realm.objectForPrimaryKey('Repository', repository.id));
    });

Error returned:

Possible Unhandled Promise Rejection (id: 17):
Error: Argument to 'delete' must be a Realm object or a collection of Realm objects.
delete@[native code]

Thanks in advance for your attention

2 answers

1

I had the same problem but I managed to solve it in the following way:

imports:

import Realm from 'realm';
import getRealm from '~/services/realm';

code:

const realmDelete = () => {

  const realm = new Realm(getRealm());

  realm.write(() => {
    realm.delete(realm.objectForPrimaryKey('Repository', repository.id));
  });

}

0


Your schema is being declared as a class:

export default class RepositorySchema {
  static schema = {
    name: 'Repository',
    primaryKey: 'id',
    properties: {
      id: { type: 'int', indexed: true },
      login: 'string',
      name: 'string',
      fullName: 'string',
      description: 'string',
      avatarUrl: 'string',
      stars: 'int',
      forks: 'int',
      watchersCount: 'int',
    },
  };
}

Try declaring your schema only as an object:

export default RepositorySchema = {
  name: 'Repository',
  primaryKey: 'id',
  properties: {
    id: { type: 'int', indexed: true },
    login: 'string',
    name: 'string',
    fullName: 'string',
    description: 'string',
    avatarUrl: 'string',
    stars: 'int',
    forks: 'int',
    watchersCount: 'int',
  },
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.