useEffect( () => {
const fetchData = async () => {
await Moralis.start({ serverUrl, appId, masterKey })
const Content = Moralis.Object.extend("Tweets");
setTweets(Content?.get('username'));
setEthAddress(Content?.get('ethAddress'));
setUserName(Content?.get('username'));
// Content.get("username");
};
fetchData();
}, [Moralis, tweets]);
Since you wrraped your application with MoralisProvider, you don’t need Moralis.start
anymore. Here’s a little fix
useEffect(() => {
const fetchData = async () => {
const Content = Moralis.Object.extend("Tweets");
const query = new Moralis.Query(Content);
const content = await query.find(); // Array of Tweets
// setTweets(Content?.get("username"));
// setEthAddress(Content?.get("ethAddress"));
// setUserName(Content?.get("username"));
// Content.get("username");
};
isInitialized && fetchData();
}, [isInitialized, tweets]);
Because content
will be an array, you should access the elements with index, that’s why other parts are commented out
but it was telling me that i need moralis.start, so please how do i know get the username
If you have isInitialized
as your dependencies and you check If isInitialized
before running the fiction, it ought not to
const [profileData, setProfileData]= useState<any|null>(null);
useEffect(() => {
const fetchData = async () => {
const Content = Moralis.Object.extend("Tweets");
const query = new Moralis.Query(Content);
const content = await query.find(); // Array of Tweets
setProfileData(content as any)
};
isInitialized && fetchData();
}, [Moralis.Object, Moralis.Query, isInitialized]);
if (isInitializing) {
return <CircularProgress/>;
}
{profileData?.get(‘username’)}
TypeError: profileData.get is not a function
type or paste code here
you can use some debugging from time to time, it works with console.log(JSON.stringify(variable_name))
in react
A little thing to note here, content
is an array, so to use .get
on any of it’s element, you need to access it through index such like profileData[0]?.get(‘username’)
which should return that of the first element
TypeError: Cannot read properties of null (reading '0')
Try console.log profileData and profileData[0] to see if you got something