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>