texas697
texas697

Reputation: 6387

how to use react-native realm

I started to use realm in my project and I have a question about retrieving the data. When I make a call to query all of the objects in my Subdivision model the return result includes the get/set methods. I must parse and stringify this before I am able to use it. I have read the docs and I do not see anything about this. Is this something that has to be done or am I missing something?

Model

    class Subdivision extends Realm.Object { }
Subdivision.schema = {
    name: 'Subdivision',
    primaryKey: 'id',
    properties: {
        id: 'int',
        subdivisionName: 'string',
        latitude: 'float',
        longitude: 'float',
        status: 'string',
        statusFlag: 'string',
        marketId: 'int',
        color: { type: 'int', optional: true },
        colorStr: { type: 'string', optional: true }
    }
};

Setting Subdivisions

     _.each(response, (item) => {
            Realm.write(() => {
                Realm.create('Subdivision', item);
            });
        });

Getting Subdivisions

 let result = Realm.objects('Subdivision');
    let strRes = JSON.parse(JSON.stringify(result))

Upvotes: 0

Views: 1097

Answers (1)

Ari
Ari

Reputation: 1447

You shouldn't have to call JSON.stringify on result. Realm.objects returns a Results object which functions very similiarly to a JS Array: https://realm.io/docs/react-native/0.13.0/api/Realm.Results.html

You should be able to access each result using subscript indexing or other JS enumerations methods, ie result[0], or results.forEach((subidv) => {...}) etc.

Upvotes: 1

Related Questions