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?
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
realm.objectForPrimaryKey('Item', item_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With