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!
question from:
https://stackoverflow.com/questions/65863848/why-my-react-redux-action-returns-undefined 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…