Problem with params in execute function

i am developing a daap where events can be created and user can buy tickets of that events
Bsc testnet(0x9759C7ec42eEd34CeA2957929EB6D46390e2A109). i have successfully implement the create event function. Now having trouble with buyTicket function.
console is showing

main.js:141 Uncaught (in promise)

1. (2) ['_id is required', 'quantity is required']

  1. 0: "_id is required"
  2. 1: "quantity is required"
  3. length: 2
  4. [[Prototype]]: Array(0)

JS file

const serverUrl = ""

  const appId = ""

   Moralis.start({serverUrl, appId})

   

   async function login() {

    let user = Moralis.User.current();

 

    user = await Moralis.authenticate({ signingMessage: ' done' });

      if(user){

        console.log(user);

        let addressOfUser= document.getElementById('address')

        document.getElementById('logn').innerHTML='connected'

        addressOfUser.innerHTML=(user.get('ethAddress'));

      }

    }

async function init() {

  const query = new Moralis.Query("Events");

  const results = await query.find();

  console.log("events", results);

  // const Monster = Moralis.Object.extend("Events");

  //  const query = new Moralis.Query(Monster);

  // const results = await query.find();

  // console.log("events", results);

  let aip= document.getElementById('udata')

  aip.innerHTML= results.map((items)=> `<div> name:${items.attributes.name}<br></br>ID:${items.attributes.uid}<br></br>

   </div>`).join('')

async function createEvent(name, date, price, ticketCount, id) {

  await Moralis.enableWeb3();

  let op = {

    contractAddress: "0x9759C7ec42eEd34CeA2957929EB6D46390e2A109",

    functionName: "createEvents",

    abi: [

      {

        inputs: [

          { internalType: "string", name: "name", type: "string" },

          { internalType: "uint256", name: "date", type: "uint256" },

          { internalType: "uint256", name: "price", type: "uint256" },

          { internalType: "uint256", name: "ticketCount", type: "uint256" },

          { internalType: "uint256", name: "id", type: "uint256" },

        ],

        name: "createEvents",

        outputs: [],

        stateMutability: "nonpayable",

        type: "function",

      },

    ],

    params: {

      name,

      date,

      price,

      ticketCount,

      id,

    },

  };

  await Moralis.executeFunction(op);

}

async function buyTicket(id, quantity){

  await Moralis.enableWeb3();

  let tic= document.getElementById('btic').value

  let tid= document.getElementById('tid').value

  let options={

    contractAddress: "0x9759C7ec42eEd34CeA2957929EB6D46390e2A109",

    functionName: "buyTickets",

    abi:[{"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"},

    {"internalType":"uint256","name":"quantity","type":"uint256"}],

    "name":"buyTickets","outputs":[],"stateMutability":"payable","type":"function"}],

    params:{

      id,

      quantity,

    },

    msgValue:Moralis.Units.ETH(tic),

  }

  await Moralis.executeFunction(options)

}

let cont = document.getElementById("cont");

let btn = document.getElementById("btn").addEventListener("click", async () => {

  var nam = document.getElementById("name").value;

  let dt = document.getElementById("date").value;

  let pri = document.getElementById("price").value;

  let ticketCount = document.getElementById("ticketCount").value;

  let id = document.getElementById("id").value;

 

  // cont.innerHTML +=

  await createEvent(nam, dt, pri, ticketCount, id);

 

});

document.getElementById('logn').onclick= login;

document.getElementById('tbtn').onclick=buyTicket;

init()
html file



=

     <!-- INSTALL THE SDK and WEB3 -->

        <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>  

        <script src="https://unpkg.com/moralis-v1/dist/moralis.js"></script>

        <!-- / -->

       

    <title>Document</title>

</head>

<body id="body">

   

   <div><button id="logn">Connect</button></div>

   <div id="address"> </div>

   <p>Create event </p>

    <input type="text" placeholder="name" id="name">

    <div><input type="text" placeholder="date" id="date"></div>

    <div ><input type="text " placeholder="price" id="price"></div>

    <div ><input type="text " placeholder="id" id="id"></div>

    <div ><input type="text " placeholder="ticketCount" id="ticketCount"></div>

   

    <button type="submit" id="btn">confirm</button>

   <div><input type="text" placeholder="id" id="btic"></div>

    <div><input type="text" placeholder="quantity" id="tid"></div>

    <button type="submit" id="tbtn">buyTicket</button>

<div id="cont"></div>

<div id="udata"> </div>

<script src="main.js"></script>

</body>

</html>

this is the function i am trying to implement

async function buyTicket(id, quantity){

  await Moralis.enableWeb3();

  let tic= document.getElementById('btic').value

  let tid= document.getElementById('tid').value

  let options={

    contractAddress: "0x9759C7ec42eEd34CeA2957929EB6D46390e2A109",

    functionName: "buyTickets",

    abi:[{"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"},

    {"internalType":"uint256","name":"quantity","type":"uint256"}],

    "name":"buyTickets","outputs":[],"stateMutability":"payable","type":"function"}],

    params:{

      id,

      quantity,

    },

    msgValue:Moralis.Units.ETH(tic),

  }

  await Moralis.executeFunction(options)

}

According to your abi, you got _id here.

So you ought to have _id here in the params not id

Also, make sure the values of the params are set too any any possible way

  params:{
    id: YOUR_ID_VALUE,
    quantity: YOUR_QUANTITY_VALUE,
  }

so i should write?
params:{

_id,
quantity,
}
Also, make sure the values of the params are set too any any possible way

 params:{
    id: YOUR_ID_VALUE,
    quantity: YOUR_QUANTITY_VALUE,
  }

i did not understand this part, could u show me?

Yes, you can use it that way. But it’s an object of key value piar, so you need to supply the keys their values too if they exist

Looks like you’re getting the values here

1 Like

another problem i am having when i try to create event with price like this, 0.01 i got this error.

moralis.js:42785 Uncaught (in promise) Error: invalid BigNumber string (argument="value", value="0.01", code=INVALID_ARGUMENT, version=bignumber/5.6.0)

how to solve it,?

whole number works fine.

Unfortunately,this behaviour isn’t supported by solidity, only integer (whole) numbers can be used.Quantity should be in whole number. You can consider calculating in wei, finney or some other lower values with some extra logic

i am talking about price in create event function, not quantity.

this one,

async function createEvent(name, date, price, ticketCount, id) {

  await Moralis.enableWeb3();

  let op = {

    contractAddress: "0x9759C7ec42eEd34CeA2957929EB6D46390e2A109",

    functionName: "createEvents",

    abi: [

      {

        inputs: [

          { internalType: "string", name: "name", type: "string" },

          { internalType: "uint256", name: "date", type: "uint256" },

          { internalType: "uint256", name: "price", type: "uint256" },

          { internalType: "uint256", name: "ticketCount", type: "uint256" },

          { internalType: "uint256", name: "id", type: "uint256" },

        ],

        name: "createEvents",

        outputs: [],

        stateMutability: "nonpayable",

        type: "function",

      },

    ],

    params: {

      name,

      date,

      price,

      ticketCount,

      id,

    },

  };

  await Moralis.executeFunction(op);

}
let cont = document.getElementById("cont");

let btn = document.getElementById("btn").addEventListener("click", async () => {

  var nam = document.getElementById("name").value;

  let dt = document.getElementById("date").value;

  let pri = document.getElementById("price").value;

  let ticketCount = document.getElementById("ticketCount").value;

  let id = document.getElementById("id").value;

 

  // cont.innerHTML +=

  await createEvent(nam, dt, pri, ticketCount, id);

 

});

You can convert the value to wei and send it to the contract that way rather

1 Like