Issue
I am trying to redirect users to different dashboards based on their roles after they sign in using Google Sign In within our Login page. However, I am encountering an error that says "TypeError: Cannot read properties of undefined (reading 'role')" because the getUserDashboardAndId function is executing before the login data gets stored in the Store. I think this is because I am using async actions and expecting sync results. Additionally, I need to keep in mind that storeValue is an async function.
Resolution
The issue with the code is that it is trying to access data from the store before the login data is stored. This is because the code is running asynchronously, while the data retrieval is happening in the background. To solve this, you can use async/await functions to ensure that the data retrieval is complete before accessing it.
Here's an example of how to update the function to make it work:
getUserDashboardAndId: async () => {
const user = await getUserData.run();
storeValue('email', user.email);
const loggedInUser = await getEmployeeByEmail.run();
storeValue("loggedin_user", loggedInUser[0]);
const role = appsmith.store.loggedin_user.role;
if (role === "Employee") {
return "employeedashboard-63d89abcb8b46679d190f4c8";
} else if (role === "Reviewer") {
return "reviewerdashboard-63e24e094fc63a0df6b867ae";
} else {
return "admindashboard-63d89abcb8b46679d190f4e5";
}
},
This code uses async/await to wait for the data to be retrieved before storing and accessing it. By doing this, it ensures that the code will not try to access data that has not yet been retrieved.