Search code examples
javascriptreduxredux-orm

"map is not a function" with redux-orm 0.9.0


I'm creating a simple example to learn the redux-orm library. I've set up my reducer as below and everything seems to work great.

const orm = new ORM();
orm.register(Profile, User);
export function reducer(state, {type, payload}) {
  const session = orm.session(state || orm.getEmptyState());
  switch(type) {
    case 'CREATE_USER':
      session.User.create(payload)
      break;

    case 'CREATE_PROFILE':
      session.Profile.create(payload)
      break;
  }
  return session.state;
}

I'm following the example to create a simple selector, that gets the entire list of users in the DB:

export const getUsers = createSelector(orm, session => {
  return session.User.map(user => {
    return user.ref
  });
});

However, upon using the selector, Chrome gives me an error: ERROR TypeError: session.User.map is not a function

Am I missing something? Or is this a bug with the library?


Solution

  • You need to use either toModelArray or toRefArray. The equivalent code to what you seem to be going for in your example (getting the refs for each User) would be:

    return session.User.all().toRefArray()
    

    If you need to do further processing, like getting relations or refining to only a subset of properties, use toModelArray and map through it as you would a normal array.