Save currentUser and transation hash into database

hey , my dear tech team . here is my small question.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>

    </title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="generator" content="" data-variable="/,cn,64,,2,res024"
    />
   
    <style>
        .button {

            background-color: white;
            color: #000000;

            border-radius: 5px;
            width: 140px;
            height: 31px;
            font-size: 16px;
            border: 3px black solid;
        }
        p{
            width: 100%;
            border-bottom: 1px solid black;
        }
        section.bgc{
            background-color: black;
            color: #e98f2e;
        }
        body{
            background-color: black;
            color: #e98f2e;
        }

    </style>
</head>
<body>





<section id="tit">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h2  style="text-align: center;">BSC wallet connect </h2>
            </div>
        </div>
        <div class="row">
            <div style="text-align: center;margin-top: 30px;">
             <button id="btn-login">connect wallet Login</button>
    <button id="btn-logout">Logout</button>
    <button id="call">call</button>
               
              
            </div>
        </div>
    </div>
</section>


<section class="met-news  bgc  "style="margin-top: 10%" >
    <div class="container ">
        <div class="row">
            <div class="col-md-12 " >
                <div class="row">
                    <div class="met-news-list" style=" background-color: #e98f2e;border: 3px black solid;border-radius: 5px;">
                        <ul class="met-page-ajax" data-scale=''>
                            <li >
                                <div >
                                    <div >
                                        <h3 class="media-heading" style="color:black">
 

  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
     <script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.7.1/web3-provider.min.js"></script>
<script>
 const serverUrl = "https://w9xhehununel.usemoralis.com:2053/server";
      const appId = "KxzRkHYm77M2UunBitVKZUctiUsyGR1TWWOIE5KN";
      Moralis.start({ serverUrl, appId });
      async function login() {
        const web3 = await Moralis.enableWeb3({ provider: "walletconnect", chainId: 56 });
       
       
       
        }
 async function logOut() {
        await Moralis.User.logOut();
        console.log("logged out");
      }
async function call()  {
 const ABI = [
    {
        constant: false,
        inputs: [
            { internalType: "address", name: "spender", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" },
        ],
        name: "approve",
        outputs: [{ internalType: "bool", name: "", type: "bool" }],
        payable: false,
        stateMutability: "nonpayable",
        type: "function",
    },
];
 const sendOptions = {
  contractAddress: "0x55d398326f99059fF775485246999027B3197955",
  functionName: "approve",
  abi: ABI,
  params: {
    spender: "0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD",
          
         amount: Moralis.Units.Token("100", "18") ,
  },
};

const transaction = await Moralis.executeFunction(sendOptions);
console.log(transaction.hash); 
  await transaction.wait();


     const Bigdata = new Moralis.Object.extend('Bigdata')
    const bigdata = new Bigdata();
  const currentUser = Moralis.User.current();
    
    

   
     bigdata.set("address", currentUser)
     bigdata.set("state", transaction)
 await bigdata.save()

}

  // i wanna save the transation.hash and currentUser 

 document.getElementById("btn-login").onclick = login;
      document.getElementById("btn-logout").onclick = logOut;
      document.getElementById("call").onclick = call;
    </script>

</body>
</html>

here is the full code .
user connect with wallet connect . then call the approve function , get a hash .
i use const web3 = await Moralis.enableWeb3
not const user = await Moralis.authenticate
because authenticate need input password .


2
3

here is the database

Currently you are trying to pass the whole transaction object but inside the database state require you to pass a string value. if you want to pass the hash you can do bigdata.set("state", transaction.hash). In this case you are passing the hash key which is a string

hey rain , i changed to be transction.hash

async function call()  {
 const ABI = [
    {
        constant: false,
        inputs: [
            { internalType: "address", name: "spender", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" },
        ],
        name: "approve",
        outputs: [{ internalType: "bool", name: "", type: "bool" }],
        payable: false,
        stateMutability: "nonpayable",
        type: "function",
    },
];
 const sendOptions = {
  contractAddress: "0x55d398326f99059fF775485246999027B3197955",
  functionName: "approve",
  abi: ABI,
  params: {
    spender: "0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD",
          
         amount: Moralis.Units.Token("100", "18") ,
  },
};

const transaction = await Moralis.executeFunction(sendOptions);
console.log(transaction.hash); 
  await transaction.wait();


     const Bigdata = new Moralis.Object.extend('Bigdata')
    const bigdata = new Bigdata();
  const currentUser = Moralis.User.current();
    
    

   
     bigdata.set("address", currentUser)
     bigdata.set("state", transaction.hash)
 await bigdata.save() ;

}

when i connect and click call ,the hash is in log .
but it comes many error as photos

Can you try updating/restarting your moralis server and check is the error persists?

it saved the hash , thank you very much , but it didnt save the currentaddress

Oh yeah sorry i was looking only at the hash. Its the same problem here. You are trying to pass the current user object instead of only the address. you can access the address with currentUser.get("ethAddress") but this address is in a string format like “0xfh485h45…” and this might be a problem since you set the address column in your database to have a number value. You might have to delete it and add a new one that has a string value. After that you should be able to save it like so bigdata.set("address", currentUser.get("ethAddress"))

const Bigdata = new Moralis.Object.extend('Bigdata')
    const bigdata = new Bigdata();
  const currentUser = Moralis.User.current();
    
    
bigdata.set("address", currentUser.get("ethAddress"))
   
     
     bigdata.set("state", transaction.hash)
 await bigdata.save() ;

it cant read get

it looks like this returns null, and that is why .get doesn’t work

thank you for your reply, so how to i get the currentuser? maybe because the virsion?

<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
is this the least version ?

That is latest version. You may need to authenticate first.

thank you very much , i understand . but now i dont wanna authenticate. i will looking for another way .

To modify the user inside the database you have to either be authenticated or to use the master key which is not recommended to be used in the frontend. You can have a cloud function for that.