import { createAsyncThunk, createSlice, createAction } from "@reduxjs/toolkit";
/* import moralis */
const Moralis = require("moralis");
const serverUrl = process.env.REACT_APP_MORALIS_SERVER_URL;
const appId = process.env.REACT_APP_MORALIS_APPLICATION_ID;
const masterKey = process.env.MASTER_KEY;
//since balanceReducer is an extraReducer function. It is created
//by createAction class
export const fetchBalance = createAsyncThunk(
"userBalance/fetchBalance",
async() => {
Moralis.start({ serverUrl, appId, masterKey });
const currentUser = await Moralis.User.current();
console.log(currentUser);
var userAddress = currentUser['ethAddress'];
console.log(userAddress)
const query = new Moralis.Query('_User');
console.log(query)
query.equalTo("ethAddress", userAddress);
const results = await query.find({ userMasterKey: true })
return results;
})
const userBalanceSlice = createSlice({
name: "userBalance",
initialState: {
value: 0,
},
reducers: {},
extraReducers: {
[fetchBalance.fulfilled] (state, { payload }) {
state.value = payload;
}
}
})
export const userBalance = (state) => state.userBalance.value;
export const balanceReducer = userBalanceSlice.reducer;
Moralis.User.current() always returns old data. Thatβs why I used Moralis.Query but the problem is not solved. I am using redux into ethereum-boilerplate-react