Moralis Objects updating inconsistently

Hey guys,

I’m currently updating the value from one table after fetching the required value from another table. This is my function:

Moralis.Cloud.afterSave("PlayersEntered", async (request) => {
  const confirmed = request.object.get("confirmed")
  if (confirmed) {
    const CreatedWhoopys = Moralis.Object.extend("CreatedWhoopys")
    const createdWhoopys = new CreatedWhoopys()
    let query = new Moralis.Query("PlayersEntered")
    
  
    const query1 = new Moralis.Query("CreatedWhoopys")
    query1.equalTo("whoopyAddress", request.object.get("address")) //try using emitted address of whoopy instead of default table param 
    const item = await query1.first()
    if(item) {
      item.set("PlayersEntered", request.object.get("currentPlayersNumber"))
      await item.save()
      logger.info("Saved!")
    } else {
      logger.info("Not Found")
    }

  } else {
    console.log("Error processing tx")
  }

  }


)

The function works correctly, and the item gets set correctly as well, however this only works one every two-three times the event is fired. Other times the value stays the same and doesn’t update, even though the log says saved.

I am currently testing on Hardhat. Is this an issue with hardhat/moralis or is there something wrong with the logic?

Thanks!

this is the part that doesn’t work every time?

you can also add an afterSave hook for “CreatedWhoopys” to see if it is called

Yes. Even though it says ‘saved’ in the logger, this is the part which doesn’t work all the time.

try to add a try catch

Try catch not bringing up anything. Also, sometimes it lags, for example the first time I enter, it won’t show ‘saved’. But the second time, it will show saved twice.

In the PlayersEntered table, everytime a player enters a whole new row is created, which repeats all values (except entry count which increases). Could it be because the values are repeated the db gets confused? Or could it have to do with hardhat?

I’m soon going to be switching over the Streams anyways, so do you think I could go about doing all this in a more efficient manner there?

with streams API you get the events data directly in your backend, but streams API doesn’t work with hardhat now

strange, maybe you can add more logging to see what is lagging

Yeah, that’s why I’m testing on hardhat for now. Also, I’m adding another value from the playersEntered table the same way and that one updates correctly on time, so it’s just the playerCount that didn’t seem to be updating.

Instead of adding the player count from there, I’m using a counter now (which works perfectly). For the counter, I had to create a column in the UI and I had to set the data type as ‘number’, otherwise it’s set to string for all my other variables.

Any idea if I can programmatically set it to number? I don’t know why it’s set to string as default event though it’s a uint256.

uint256 can not be represented properly as number in the database
This functionality is from parse server with cloud code and the database is mongo db in particular

1 Like