[SOLVED] How to increment a specific item in a table using .increment?

Hey guys,

As per the docs, to increment the counter, one needs to pass in a function like this:

monster.increment("energy");
monster.save();

But what if I wanted to increment the energy for a specific monster in the monster table? How would I do that?
Thanks!

you can do first a search with find, get current value, increase it with 1 and then save the new value

Hey @cryptokid,
This is the function I am currently using:

Moralis.Cloud.afterSave("PlayersEntered", async (request) => {
//get address of whoopy which player entered
  const confirmed = request.object.get("confirmed")
  if (confirmed) {
    const CreatedWhoopys = Moralis.Object.extend("CreatedWhoopys")
    const createdWhoopys = new CreatedWhoopys()
    const query = new Moralis.Query("PlayersEntered")
    query.equalTo("address", request.object.get("address"))
    const result = await query.first()

//Find corresponding row in Created Whoopys table based on address from previous query
    const query1 = new Moralis.Query("CreatedWhoopys")
    const currentValueRow = query1.equalTo("address", result)
    currentValueRow.increment("playersEntered")
    await currentValueRow.save()

What I want to do is basically increment the value of Players entered in the CreatedWhoopys table, however I want to specifically update the value based on whoopy address which I get in the PlayersEntered table. Both tables have an ‘address’ column.

I have queried the whoopyAddress from PlayersEntered table but now I can’t figure out how to increment the playerEntered value in the CreatedWhoopys table according to that particular result.

Is it possible for me to do a function similar to this:

//increment the column players entered in that specific row
currentValueRow.increment("playersEntered")

Thanks!

using a find and .set doesn’t work for you?

you run a query in a table to get current value, you increment it and then you save the new value

I’m not entirely sure of the syntax. This is what I’m currently doing:

Moralis.Cloud.afterSave("PlayersEntered", async (request) => {
  const confirmed = request.object.get("confirmed")
  if (confirmed) {
    const CreatedWhoopys = Moralis.Object.extend("CreatedWhoopys")
    const createdWhoopys = new CreatedWhoopys()
    const query = new Moralis.Query("PlayersEntered")
    query.equalTo("address", request.object.get("address"))
    const result = await query.find()


    const query1 = new Moralis.Query("CreatedWhoopys")
    const currentValueRow = query1.containedIn("address", [result])
    currentValueRow.increment("playersEntered")
    await currentValueRow.save()

and I get the following error:

currentValueRow.increment is  not a function 

Thanks!

I’m not saying to use increment syntax. You can use a simpler syntax, you search for the entry, you increase the field and then you save the entry.

I can search for the entry, but when I set it, it gets saved to a new row. How can I save it in the existing row?

This should be easy. You have to save the same object that was the result from the search and not to create a new object.

I got it!! I used the following logic I got from another answer on here:

  const query = new Moralis.Query("ItemsForSale");
  query.equalTo("id", "id0001");

  const item = await query.first();
  if(item){
     item.set("status", "sold");
     await item.save();
   }

It was really simple but I don’t think setting variables is explicitly covered in the docs.

Regardless, thank you so much!

1 Like