Lesson 10: Getting "Undefined" for the getEntranceFee() function** #5835
-
Issue: Getting "Undefined" for the getEntranceFee() functionHi, I have checked similar discussions and solutions, but they did not work.
I would be appreciated if you could help me. My all files are here (Github Repo): // have a function to enter the lottery
import { useWeb3Contract } from "react-moralis"
// import { Contract } from "web3uikit"
import { abi, contractAddresses } from "../constants"
import { useMoralis } from "react-moralis"
import { getWrappedNative } from "web3uikit"
import { useEffect } from "react"
export default function LotteryEntrance() {
const { chainId: chainIdHex, isWeb3Enabled, Moralis } = useMoralis()
const chainId = parseInt(chainIdHex) // "PaeseInt" make the number from hex code
const raffleAddress = chainId in contractAddresses ? contractAddresses[chainId][0] : null
// console.log(raffleAddress)
// const { runContractFunction: enterRaffle } = useWeb3Contract({
// abi: abi,//,
// contractAddress: raffleAddress,
// function:"enterRaffle",
// params: {},//
// msgValue://
// })
const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi, //,
contractAddresses: raffleAddress,
function: "getEntranceFee",
params: {}, //
})
let entranceFee = ""
useEffect(() => {
if (isWeb3Enabled) {
async function updateUI() {
// const something = await getEntranceFee()
// console.log(something)
entranceFee = await getEntranceFee()
console.log(entranceFee)
}
updateUI()
}
}, [isWeb3Enabled])
return (
<div>
Hi from Lottery entranceFee!<div>{entranceFee}</div>
</div>
)
} The problem part is here. useEffect(() => {
if (isWeb3Enabled) {
async function updateUI() {
// const something = await getEntranceFee()
// console.log(something)
entranceFee = await getEntranceFee()
console.log(entranceFee)
}
updateUI()
}
}, [isWeb3Enabled]) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
1- the code is fine, just missed this const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "getEntranceFee",
params: {},
}); 2- you can use const [entranceFee, setEntranceFee] = useState(""); after changes will be like this const entranceFeeFunction = async () => {
const entranceFee = await getEntranceFee();
setEntranceFee(entranceFee.toString());
console.log(`entranceFee ${entranceFee}`);
};
useEffect(() => {
if (isWeb3Enabled) {
async function updateUI() {
entranceFeeFunction();
}
updateUI();
}
}, [isWeb3Enabled]); I hope this helps |
Beta Was this translation helpful? Give feedback.
-
note: don't push |
Beta Was this translation helpful? Give feedback.
1- the code is fine, just missed this
functionName:
instead offunction
,and I think it is
contractAddress
notcontractAddresses
like this
2- you can use
useState
hook instead of using native variables.after changes will be like this