[SOLVED] Moralis Queries: How to query data from one table using parameter from other table?

Hey guys,

I am currently querying a table to receive an ascending list as follows:

  const { data: listedWhoopys, isFetching: fetchingListedWhoopys } =
  useMoralisQuery("CreatedWhoopys", (query) =>
    query.limit(100).ascending("whoopyName")
  );
  console.log(listedWhoopys);

I then pass this to my front end by mapping every single parameter as follows:

        {listedWhoopys.map((whoopy) => {
                    console.log(whoopy.attributes)
                    const { whoopyName, whoopyColor, maxSpeed, maxHeight, interval, creatorAddress, whoopyAddress } =
                    whoopy.attributes
                    return (
                        <WhoopyCard
                            whoopyName={whoopyName}
                            whoopyColor={whoopyColor}
                            maxSpeed={maxSpeed}
                            maxHeight={maxHeight}
                            interval={interval}
                            creatorAddress={creatorAddress}
                            whoopyAddress={whoopyAddress}
                            playersEntered= ** HOW TO ADD THIS?
                        />
                    )
                })

WhoopyCard is a component and I pass the parameters into it as props. Everytime a new Whoopy is created, a new card is automatically created with respective parameters.

Whoopy is a game, and I would like to keep track of the people entered for every individual whoopy so that that number shows in every WhoopyCard component. However, when a player enters, the event that updates the player count is recorded in another table and not the “CreatedWhoopys” table.

Now my question is this: how can I pass the player count from the other table to the WhoopyCard component? The WhoopyAddress parameter is present in both tables, and I’d need this to ensure that the relevant WhoopyCard updates correctly. Also, while all the other parameters in the WhoopyCard are supposed to stay the same, I want the player count to keep increasing.

How can I add the player count (from the other table) as a prop in WhoopyCard so that it updates correctly?

NOTE: Both tables have been created using core_AddEventsSync since I am recording different instances of the same contract.

Please let me know if any additional info is required.
Thanks!

If the Whoopy table relates to the second, then you could include the other table’s data with relational queries.

What does the second table look like? Or how have you “linked” the two tables together?

The two tables aren’t linked together. Both are created and updated using core_addEventsSync.

Let me explain the order of events once again. First, a player creates the Whoopy (using factory contract). This emits an event with the whoopy address and attributes and all these are saved to a table called CreatedWhoopys. Now that the Whoopy is created, other players can join it. When they join it, another event is emitted with their address, the Whoopy name, the whoopy address and also the player count of the whoopy. This event however is saved to another table called PlayersEntered.

The front end has a whoopycard with all this info from both events. I have successfully got everything into the whoopycard from the first event, however I can’t figure out how to update player count from the second event with respect to relevant whoopy address.

I have been researching this, and I made some progress. I am able to get the player count into the CreatedWhoopys table using and afterSave trigger for when the PlayersEntered table is updated, however there is an issue with this since the player count is always added to a new row. This would be highly frustrating since multiple players will be entering many different whoopys, so it might as well be random numbers since I can’t do anything with the data.

I am wondering if there’s a way I can some how link the two tables via whoopyAddress(both tables have this value and it will always be unique) and make it so that when a player enters, the playercount is automatically updated in the CreatedWhoopys table.

I know this might be slightly confusing so please let me know if you need any more info.
I’ve added pictures of the two tables for context.

Thanks!

Hey @alex

How can I get the data to my front end after ‘including’ my other tables data. All of this is being done in cloud functions since I need it to be done afterSave.

Here is what I’ve done:

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")

    query.equalTo("currentPlayersNumber", request.object.get("currentPlayersNumber"))

    const query1 = new Moralis.Query("CreatedWhoopys")
    query1.equalTo("whoopyAddress", request.object.get("address"))
    query1.matchesQuery("playersEntered", query)
    query1.include("playersEntered")

    // createdWhoopys.set("playersEntered", count)
  
    // await createdWhoopys.save()

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

  }
  


)

Is there a way for me to save this data to the Whoopy table and then query from front end or do I have to directly query it?

Any help would be appreciated :slight_smile: Thanks!

Hey @alex,
I figured it put!

I simply got the number from the second database and set the query accordingly in the first one.

Thanks!!

1 Like