Hi guys, I’m trying to fetch data from my db with moralis and set the response to what I get.
const { isInitialized, Moralis } = useMoralis();
const [isLoading, setIsLoading] = useState(false);
let [eventInfo, setEventInfo] = useState(null);
let [error, setError] = useState(null);
const params = useParams();
useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
try {
const query = new Moralis.Query('ContractCreated');
query.equalTo('contractAddress', params.contractId);
await query.find()
.then(response => {
console.log(response[0].attributes)
setEventInfo(response[0].attributes)
console.log(eventInfo);
});
}
catch (error) {
console.log(error)
setError(error);
}
setIsLoading(false);
}
fetchData()
}, [isInitialized]);
the response[0].attributes is what I need to set eventInfo to.
However when I do setEventInfo(response[0].attributes) and that later on I render this I get an error because it says eventInfo is undefined.
return (
<div style={{ paddingTop: '50vh' }}>
<h1 style={{ color: 'black' }}>Event {params.contractId}</h1>
<div style={{color:'black'}}>
<p>{eventInfo.eventName}</p>
</div>
</div>
)
TypeError: Cannot read properties of null (reading ‘eventName’)