Fixing “cannot convert number to sequence” in RethinkDB

If you don’t use the correct string of predicates in RethinkDB, you can get an error like this:

Server error:

e: Cannot convert NUMBER to SEQUENCE in:
r.db("test").table("users").filter(function(var_229) { return var_229("user_name").match("test "); }).group("first_name").count().map({"first_name": r.row("group"), "val": r.row("reduction")})
^^^^^^^

Here is an example of a query that causes this:

r.db('test')
 .table('users')
 .filter( (x) => x('user_name').match('test ') )
 .group('first_name')
 .count()
 .map({ first_name: r.row('group'), val: r.row('reduction') })

The problem with this query is that the output of the grouped “count” is not the same datatype as a regular table. To convert it back, you need to add an additional “ungroup”:

r.db('test')
 .table('users')
 .filter( (x) => x('user_name').match('test ') )
 .group('first_name')
 .count()
 .ungroup()
 .map(
   { 
    first_name: r.row('group'),
    val: r.row('reduction') 
  })