Accessing database in front end

Hello everyone,
I have a smart contract which has a deposit event. I have added it to sync and watch smart contract events successfully and able to get the events in dashboard also. Is it possible to read these events in front-end? may be using cloud functions?
currently I am reading events using this.contract.events.Deposit but when multiple events are happening the order of events won’t be proper or events won’t be emited. So I want to try if its possible to read from moralis database.
Thank you.

it is possible to use a cloud function to get the data from the database

I tried it and yes its possible. I added the following in cloud function

Moralis.Cloud.define("Deposit", async (request) => {
    const query = new Moralis.Query("Deposit");
    // query.equalTo("confirmed", true);
    const results = await query.find();
    return results;
});

and in front-end added the following code.

let result = await Moralis.Cloud.run("Deposit");
console.log("results: ", result);

But I also want to get this every time deposit happens. Is that possible?

You mean you want to use a live query to get the data form that table in front end?

yes. Is that possible?

Yes, there is live query functionality, you can find example in documentation

Okay, will take a look at that.
Thank you

Can you please tell me how do I use it?
I tried the following in front-end but didnt work:

const deposiQuery = new Moralis.Query("Deposit");
const deposiQuerySubscription = await deposiQuery.subscribe();
deposiQuerySubscription.on('create', ()=>{
console.log("deposit successfull")});

Try on multiple events like on update, on create may not work on a core services server where on update will work.

1 Like

using update is working fine. Thank you.
I want to know what confirmed column is in Moralis database. Because I get log 2 times when deposit is successful. first when confirmed column is false and another one when its updated to true.

it may mean when the transaction is confirmed, it may be easier to differentiate between the equivalent of create and an update than using confirmed field, try to use console.log for that data that you receive from that update event

From what I have noticed, an entry will be made to database when the transaction is complete and confirmed will be false, and after a minute or two confirmed will be set to true. Now I am checking if confirmed field is false then I will log, after it updates to true, it will be ignored. This can be done right?
What does confirmed column is for?
Can that column be deleted in database?

it looks like that confirmed means when the transaction is confirmed on blockchain, a transaction is considered as confirmed on blockchain when a number of 10-20 (it depends on chain) blocks were mined after that transaction was included on chain, blocks that confirm that transaction

1 Like