Fixing “Expected type TABLE but found SELECTION” in RethinkDB

If you write a query that tries to compare two datasets, you may get errors:

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

Like so:

e: Expected type TABLE but found SELECTION:

Clearly this indicates that we’re trying to compare two different dataset types.

The above query is attempting to filter one table by another. To do this correctly, try this:

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