Fixing “Expected type DATUM but found SELECTION” in RethinkDB

If you run a RethinkDB query, you can get an error attempting to intersect two tables:

r.db('test')
 .table('users')
 .filter( (x) => x('user_name').match('test ') )
 .setIntersection(
   r.db('test')
    .table('users')
    .filter(
      (x) => x('user_name').match('9')
    )
 )

The error looks like so:

Expected type DATUM but found SELECTION

The reason you get this error is that you have attempted to intersect two tables, when the function expects two arrays.

The simplest way to do this is to join the data on it’s IDs.

r.db('test')
 .table('users')
 .eqJoin(
     'id',
     r.table('users')
   )

If you actually want to do this on all columns, you’ll need to implement a more complex filter, that looks at each column.

You can also get this error if you try to alias columns in the incorrect way:

r.db('test')
 .table('salaries')
 .group('role')
 .avg('salary')
 .ungroup()
 .map({group: 'Title', reduction: 'Amount'})

Instead, you want this:

r.db('test')
 .table('salaries')
 .group('role')
 .avg('salary')
 .ungroup()
 .map({
   group: r.row('Title'), 
   reduction: r.row('Amount')
  })

Leave a Reply

Your email address will not be published. Required fields are marked *