Realm-js: Get Length Of List

Created on 8 Mar 2018  路  1Comment  路  Source: realm/realm-js

Goals

Get the count/length of a Realm List

Actual Results

Undefined is not an object

Steps to Reproduce

Meta.schema = {
  name: 'Meta',
  primaryKey: 'id',
  properties: {
    id: 'string',
    fileData: 'FileData[]'
  }
}

let meta = realm.objects('Meta').filtered('id = $0', '123')
let fileData = meta.fileData

console.log(fileData.length) // should output an integer 

Version of Realm and Tooling

  • Realm JS SDK Version: Latest
  • Node or React Native: Latest React Native
  • Client OS & Version: iOS
  • Which debugger for React Native: ?/None

Most helpful comment

realm.objects('Meta').filtered('id = $0', '123') returns a _collection_ of objects that match the given predicate. When you're doing:

let meta = realm.objects('Meta').filtered('id = $0', '123')
let fileData = meta.fileData

You're asking for the fileData property on this collection, which isn't a property that exists. What you'd want to do instead is to retrieve an element from the collection, then access its fileData property.

Or since id is your primary key you could just take the more direct route and use Realm.objectForPrimaryKey:

let meta = realm.objectForPrimaryKey('Meta', '123')

>All comments

realm.objects('Meta').filtered('id = $0', '123') returns a _collection_ of objects that match the given predicate. When you're doing:

let meta = realm.objects('Meta').filtered('id = $0', '123')
let fileData = meta.fileData

You're asking for the fileData property on this collection, which isn't a property that exists. What you'd want to do instead is to retrieve an element from the collection, then access its fileData property.

Or since id is your primary key you could just take the more direct route and use Realm.objectForPrimaryKey:

let meta = realm.objectForPrimaryKey('Meta', '123')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

emrehayirci picture emrehayirci  路  3Comments

texas697 picture texas697  路  3Comments

matt2legit picture matt2legit  路  3Comments

ugendrang picture ugendrang  路  3Comments

max-zu picture max-zu  路  3Comments