E
E
evg_962018-03-03 08:27:09
React
evg_96, 2018-03-03 08:27:09

Why can't I query the user from the state?

There is a collection in the users state and an auth.user collection with an authorized user.
Tried writing a selector to request from users to request an authorized user.
But for some reason it returns undefined...

export const state = state => state;
export const getCurrentUser = createSelector(state, state => {
  return state["users"].entities.forEach(user => {
    if (user.email === state["auth"].user.email) {
      return user;
    }
  });
});

class User extends React.Component {
  getDate(date) {
    return new Date(date);
  }

  render() {
    console.log(this.props.user.uid); // 2 раза выводит undefined.

    return (
      <div>
        <h1>Информация о пользователе</h1>
        <hr/>
        {
          !this.props.loaded
          ? <Loader />
          : <table className="table table-hover">
              <tbody>
                <tr><th>Имя:</th><td> { this.props.user.name }</td></tr>
                <tr><th>Почта:</th><td> { this.props.user.email }</td></tr>
                <tr><th>Телефон:</th><td> { this.props.user.phone }</td></tr>
                <tr><th>Адрес:</th><td> { this.props.user.address }</td></tr>
                <tr><th>Потраченная сумма:</th><td> { this.props.user.totalSpend } тенге</td></tr>
                <tr><th>Количество заказов:</th><td> { this.props.user.ordersCount }</td></tr>
                <tr><th>Дата регистрации:</th><td> { this.getDate(this.props.user.registeredAt).toLocaleString("ru") }</td></tr>
              </tbody>
          </table>
        }
      </div>
    );
  }
}

export default connect(
  state => ({
    loaded: state["users"].loaded,
    user: getCurrentUser(state) // state["users"].entities.valueSeq().toArray()[0] так получается пользователя взять, но нужно именно авторизованного
  })
)(User);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-03
@evg_96

return state["users"].entities.forEach(user => {
  if (user.email === state["auth"].user.email) {
    return user;
  }
});

Seriously? Are you aware that forEach returns undefined? Probably worth using find:
return state["users"].entities.find(user => user.email === state["auth"].user.email);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question