[SOLVED] Not being able to use the value of `account` as value for other function

Hi everyone,
So I am trying to return a Signature based on the account address that is connected. Like so,

const { account, Moralis } = useMoralis()

 console.log(account)

    function findSignature(address, mintSignature) {
        let data = responseData
        for (var i = 0; i < data.length; i++) {
            if (data[i]._id == address) {
                if (mintSignature in data[i]) {
                    return data[i][mintSignature]
                } else {
                    return "No Signature found"
                }
            }
        }
        return "No such address"
    }
    let keyValue = findSignature(account, "mintSignature")

    console.log(keyValue)

If I let this bit of code try to find the Signature associated with the account, it won’t work, return “No such address”, when there is the address in the given object. But if I directly input the key Address in order to find the signature, it will work and return the expected signature. What I find confusing is that I can console.log(account) and it will return the account in the console.


And here if I hardcode the account address instead of using the account variable, it will work as expected…
So instead of
let keyValue = findSignature(account, "mintSignature")
I will have to write
let keyValue = findSignature("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "mintSignature")
and then it returns the proper signature…

Does anyone know what might be the issue here ? Or how to use account as a variable for other functions ?

account will initially have a value of null - you should call findSignature in a useEffect when the value is valid (has an address).

useEffect(() => {
  if (account) {
    let keyValue = findSignature(account, "mintSignature");

    console.log(keyValue);
  }
}, [account]);
1 Like

Thank you for the reply, it seems like using useEffect does make sense but I am still unable to retrieve the signature…


In the console, it first returns undefined, I guess it is because if (account) is returning false at first. Then once the account is valid, it returns “No such address” which means that it is still unable to find the address in the object.
Somehow it is still fails to use the latest account value and it to search through the data object. Any idea what I might be doing wrong ? Or how to tweak in order to make use of the account value ?

What is responseData here?

the addresses and signatures under Json format


[{

      ...
            _id: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",

            mintSignature:

                "0xf928250a218ce40664e91b7a15d71bbe2e2e7e909006e5df7e75a39fcb8caa166b08b09668e2070752d27329a656582a70839de1b846494a9b186a6623aae4ba1b",

        },

        {

            _id: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",

            mintSignature:

                "0xec6fe39b5edb5700b6f62aed00d7435d3c0e4924089f77cc39dec63b40724741191969b63727c8c0e2874525402cc00aa5609b94e976b56110e25038c6c2a5a41b",

        },

        {

            _id: "0x17F6AD8Ef982297579C203069C1DbfFE4348c372",

            mintSignature:

                "0x0d86b284143a68635dad3e32059143ad78fec114ff20160d49d547b4c55d4223270127b212f5fffed0aa081065c868417f40b5fafb60c4602c7415bdcba3dc3f1b",

        },

        {

            _id: "0x90F79bf6EB2c4f870365E785982E1f101E93b906",

            mintSignature:

                "0x9c21e5219197dddb7ca24617ddba012b2b715af5997883643030a95a95803efc0a5a19382a47dec18d87446cfb2ec80ce690e3e355dbab74f7cc87191a0a8c0c1b",

        },

        {

            _id: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",

            mintSignature:

                "0x19ba45ee8546f6bf5305b87cc64688d091f12d8105604e376456404a3330220049c330b750aeb571ae22f1811c5cbc72760b74aa3c7e5d5d97182861e25d2ffc1b",
     ...
        }]

Then once the account is valid

How are you calling findSignature now after account is valid? Console.log address inside findSignature to make sure there’s a valid value being passed into it.

Isn’t findSignature getting fired by being inside the useEffect hook if the value of account changes ?
After console.log address or account in findSignature the connected address is logged out

After console.log address or account in findSignature the connected address is logged out

I’m not sure what you’re saying here, are you saying there is no valid address passed into findSignature when it is called?

Add another console.log here as well:

useEffect(() => {
if (account) {
// log account
}
}, [account]);

account also shouldn’t be undefined but null, that may be coming from somewhere else.

Also another issue is that your JSON addresses are using the checksum versions but account will be in lowercase.

0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

0xf39f...
1 Like

I meant that it is using a valid address, sorry for the confusion.

Yes you’re right, it was coming from something else.

Thank you that was the issue all along, now it is working fine !! Wish you have a good day mate