Reputation: 138
I'm trying to get user_id from an async function. This is my action:
export const loginCC = (email, password) => {
return async (dispatch) => {
async function CC_Session() {
return ConnectyCube.createSession({
email: email,
password: password,
});
}
try {
const response = await CC_Session();
const uid = response.user_id; //THIS GIVES THE USER ID WITHOUT A PROBLEM
const token = response.token; // THIS GIVES THE DATA TOO
dispatch({
type: LOGIN_TO_CONNECTYCUBE,
ccid: uid,
ccToken: token,
});
} catch (error) {
console.log(error);
}
};
};
And this is my reducer:
const initialState = {
token: null,
userId: null,
ccid: null,
ccToken: null,
isConnected: null,
};
export default (state = initialState, action) => {
switch (action.type) {
case LOGIN_TO_CONNECTYCUBE:
return {
ccid: action.ccid,
ccToken: action.ccToken,
};
I can get data from async function, and dispatch it.
But when I want to access it with getState().auth.ccid
, it returns "undefined".
How can I solve this problem?
Thanks!
Upvotes: 1
Views: 158
Reputation: 138
I solved the problem. I got a Firebase login right after this action. Looks like the Firebase login action resets the state. I created another reducer called user. And dispatched the action above to that user reducer. I don't know why Firebase action resets the state, but it worked after dividing reducers.
Upvotes: 1