1

I have two simple tables:

Users Table:

{
"activated": true ,
"created": Wed Jun 22 2016 15:22:32 GMT+00:00 ,
"email": email@email.com, »
"id":  "5071f620-576a-49c0-ae84-e9f32db2a034" ,
"roles": [
"Admin"
] ,
"sso":  "123456789"
}

Roles Table:

{
"id":  "f62b1d29-84d1-4bad-a359-dacb8678f607" ,
"permissions": [
"add" ,
"delete"
] ,
"role":  "Admin"
}

I am running a query to get the permissions of a given user. I am doing the following:

r.db('content').table("users").merge(function(user){
  return r.object(
    'permissions', user('roles').eqJoin(function(id){
      return id
    }, r.db('content').table('roles')).zip()('permissions')
    .reduce(function(left, right){
      return left.merge(right)
    })
    )      
});

But I am getting an error:

Server error:

e: Cannot reduce over an empty stream

What am I doing wrong and why? My desired output will give me a user with their permissions.

ApathyBear
  • 6,991
  • 12
  • 48
  • 84

1 Answers1

1

The problem is that it's trying to look for a document in the roles table with an id of Admin rather than a role of Admin. Assuming you have an index on role in the roles table (if you don't you can add one by writing r.db('content').table('roles').indexCreate('role')), you should be able to fix it by adding {index: 'role'} to the eqJoin call.

r.db('content').table("users").merge(function(user){
  return r.object(
    'permissions', user('roles').eqJoin(function(id){
      return id
    }, r.db('content').table('roles'), {index: 'role'}).zip()('permissions')
    .reduce(function(left, right){
      return left.merge(right)
    })
    )      
});
mlucy
  • 5,046
  • 1
  • 14
  • 20
  • Can you provide a full example, mainly, could you show what you mean by "adding `{index: 'role'}` to the eqJoin call" – ApathyBear Jun 27 '16 at 20:51
  • 1
    Edited the original answer. You literally just put `{index: 'role'}` at the end of the `eqJoin` call. – mlucy Jun 28 '16 at 05:57