Trouble retrieving historical transactions in React

Hello! Im trying to get historical BSC transactions after logging in through MM and getting an empty array back.
I successfully log in and get my user name from Moralis
I also see all my BSC transactions in the Moralis dashboard…any ideas what Im doing wrong here?

  const { authenticate, isAuthenticated, user, Moralis, logout, isInitialized } = useMoralis();
  console.log(isInitialized);   // returns true

  const displayUserName = () => {
    if (!isAuthenticated) return '';
    console.log(Moralis.User.transactions);    // returns undefined
    console.log(Moralis.User.current);

    return user.get('username');
  };

  const getUserTransactions = async (user: any) => {
    // create query
    const query = new Moralis.Query('BscTransactions');
    query.equalTo('from_address', user.get('bscAddress'));

    // run query
    const results = await query.find();
    console.log('user transactions:', results);  // returning []
  };

  const getStats = () => {
    const user = Moralis.User.current();
    if (user) {
      getUserTransactions(user).then((resp) => console.log(resp));  // undefined
    }
  };

1 Like

Hey @ACBassman

Take a look at Quick start User object Article

You should set a global constant:

const user = Moralis.User.current();

For getting info like: ethAddress, username … You should use attributes

const user = Moralis.User.current();

const userAddress = user.get("ethAddress");
const username = user.attributes.username;

Hope this helps you :wink:

@Yomoo Thank you for the quick response!!
Is the user you are setting for the global different from the user pulled from the useMoralis() method below?

gettransactions

No, this is the same. And it is better to use user from useMoralis()

But before using user from useMoralis() in your code
Always do a check if(isAuthenticated)

1 Like

Ahh gotcha, thank you for the quick response. I am doing this check at login and rendering username to ensure authentication.
I’m still a little confused because this is from the quickstart tutorial

    // create query
    const query = new Moralis.Query('BscTransactions');
    query.equalTo('from_address', user.get('bscAddress'));

    // run query
    const results = await query.find();
    console.log('user transactions:', results);  // returning []
  };

  const getStats = () => {
    const user = Moralis.User.current();
    if (user) {
      getUserTransactions(user).then((resp) => console.log(resp));  // undefined
    }
  };```

I recommend you watch the video I CLONED ZERION IN 20 MINUTES. From there you can get a lot of useful stuff for Moralis development on React . :wink:

1 Like

It’s okay, you should use ethAddress because it used for transactions on all chains

query.equalTo('from_address', user.get('ethAddress'));
1 Like

Ahhh good to know - that did it!! More documentation around the different get types would be really helpful :nerd_face:
Also, this projects that the video references ( https://github.com/MoralisWeb3/react-moralis#usemoraliscloudfunction ) is not the react boilerplate available here - https://docs.moralis.io/getting-started/demos and would be super helpful!

This is true, not all demos from the github are included in the documentation :face_with_monocle:
The Moralis documentation is constantly being updated and expanded.

Happy BUIDLing :wink: