Answer the question
In order to leave comments, you need to log in
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
return state["users"].entities.forEach(user => {
if (user.email === state["auth"].user.email) {
return user;
}
});
return state["users"].entities.find(user => user.email === state["auth"].user.email);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question