[SOLVED] Class object not being saved in cloud function

Hello everyone.

I am still new to moralis but so far i was able to successfuly do what i wanted (mostly simple database manipulations triggers by web3 syncs).

I have a bunch running and so far, its working as intended.

But… now im facing an error I cannot explain so Im turning to you for help.

The following code is supposed to created a gift coupon after the contract event is being catch and recorded in the buyGiftEvent table.

This is working fine as the logs tells, all the logger calls are logged propperly, but no new gift code is created in the database.

Am i missing something somewhere ?
I feel i am doing exactly like my others functions that work great.


Moralis.Cloud.afterSave("buyGiftEvent", async function (request) {

  // const tokenId = request.object.get("tokenId");

  const confirmed = request.object.get("confirmed");
  if (confirmed) {
    const classGift = request.object.get("class");
    const tx = request.object.get("transaction_hash");
    // const giftCode = request.object.get("giftCode");
    const user = request.object.get("user");
    logger.info(` buy gift entry : ${user}  -  ${classGift} - ${tx}` )
    const GifCode = Moralis.Object.extend(
      "giftCode"
    );
    const giftCode = new GifCode();
    giftCode.set("user", user.toLowerCase());
    giftCode.set("txHash", tx);
    giftCode.set("userClaim", undefined);
    giftCode.set("walletClaim", undefined);
    giftCode.set("dateClaim", undefined);
    giftCode.set("claimed", false);
    giftCode.set("typeCard", "buyGift");
    giftCode.set("verified", false);
    giftCode.set("classGift", classGift);
  
    const giftACL = new Moralis.ACL(Moralis.User.current());
    giftACL.setPublicReadAccess(false);
    giftACL.setPublicWriteAccess(false);
    giftCode.setACL(giftACL);
  
    giftCode.save(null,{useMasterKey:true})
    logger.info(` buy gift created`)

    } else {
      // handle unconfirmed case
      logger.info(`unconfirmed buy gift entry`)
    }
})

Here are the logs when i call the function (there is nothing in the errors logs) :slight_smile:

2022-10-01T22:25:06.462Z - unconfirmed buy gift entry

This one trigger when the sync is logged but still unconfirmed

2022-10-01T22:33:28.272Z -  buy gift created
2022-10-01T22:33:28.267Z -  buy gift entry : 0x599ae0c9bccb202778022efdd0eda5ebc9ee8d69  -  0 - 0x726dd18bba57e7519f200a0bc22bda5294d46abc9d73ed0feb8a223592e49ae0

So we know that the sync is working fine since the logger can get all the variables im trying to save.
But no new object in my giftCode class as you can see here (the type should be “buyGift”).

I have no clue ><

Thanks for any help :slight_smile:

Try adding a catch to this save to see if there’s an error when you try to save the object.

1 Like

something like that :


    giftCode.save(null,{useMasterKey:true})
    .then((error)=>{
      logger.info(` buy gift created`)
    })
    .catch((error)=>{
      logger.error(` gift save error :  ${error}`)
    })

?

all good, found the error now :

gift save error : ParseError: 111 schema mismatch for giftCode.classGift; expected Number but got String

I didnt thought i needed to catch that error, I totally assumed this would show up naturally.

Very good to know :slight_smile:

Thanks a lot

1 Like