Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find object by realm.js

Hello I use realm database with react-native.

I have Item schema and find one item by id.

var items = realm.objects('Item');
var item = items.filtered('id == $0', item_id);
console.log(item.name); // It should be printed name, but undefined

I can't find item. so use lodash.

var item = _.find(realm.objects('Item'), _.matchesProperty('id', item_id));
console.log(item.name); // print "ABCD"

How do I get item by id?

like image 379
ChangJoo Park Avatar asked Mar 02 '16 18:03

ChangJoo Park


2 Answers

filtered returns a Results object which is very similar to a javascript Array.

So your code should be:

var items = realm.objects('Item').filtered('id == $0', item_id);
var item = items[0];
console.log(item.name); // should print the name
like image 158
Ari Avatar answered Oct 30 '22 22:10

Ari


realm.objectForPrimaryKey('Item', item_id)

like image 31
Josh Parrett Avatar answered Oct 30 '22 22:10

Josh Parrett