Dont update table please help

please help I try to update the table but I don’t succeed, what am I wrong?


Moralis.Cloud.afterSave("Trasactions", async (request) => {

  const obj = JSON.stringify(request.object)

  const myObj = JSON.parse(obj);

  const query = new Moralis.Query("ItemsMinted");

  query.equalTo( "tokenId", myObj.tokenId );

  const object = await query.first({ useMasterKey: true });

  logger.info("object", JSON.stringify( object ) );

  logger.info("myObj", JSON.stringify( myObj ) );

  logger.info("token_id", myObj.tokenId );

  const tokenIdItemsMinted = object.get('ownerAddress')

  if ( myObj.from != myObj.to && tokenIdItemsMinted != myObj.to && myObj.from != '0x0000000000000000000000000000000000000000' ){

    logger.info("entro en compra");

    logger.info("object",  JSON.stringify(object));

    const query2 = new Moralis.Query("ItemsMinted");

    query2.equalTo( "tokenId", myObj.tokenId );

    const object2 = await query.first({ useMasterKey: true });

    object2.set('forSale', false);

    object2.set('ownerAddress', myObj.to );

    await object2.save({ useMasterKey: true });

  }

    logger.info("finish process");

});

Hi @davidzuccarini,

While saving the object use the first param as null .save(null, {useMasterKey: true})
I think that should solve it.

1 Like

If I tried it that way too and I can’t get it to run, what caught my attention is that if I put the afterSave to a table it is not saved, I have some questions, 1 if I put an afterSave or a beforeSave to a table It is not saved if what is inside is not fulfilled? 2 There are other methods that are only executed when a table is created. For this, it would be an afterSave, right? 3 If I have more than one afterSave for the same table, only one is executed per table or can I put several afterSaves?

Only one will be executed from what I know

ok then I put this code to another one that does not contain afterSave and does not store the data for me the cloud does not change.

Moralis.Cloud.afterSave("PolygonNFTTransfers", async (request) => {

  const tokenIdTransfer = request.object.get("token_id");

  const toAddress = request.object.get("to_address");

  logger.info("tokenIdTransfer", tokenIdTransfer );

  logger.info("toAddress", toAddress );

 

  const query = new Moralis.Query("ItemsMinted");

  query.equalTo( "tokenId", tokenIdTransfer );

  const object = await query.first({ useMasterKey: true });

  logger.info("object", JSON.stringify( object ) );

  const tokenIdItemsMinted = object.get('ownerAddress')

 

  if (  tokenIdItemsMinted != myObj.from_address ){

    logger.info("entro en compra");

    const query2 = new Moralis.Query("ItemsMinted");

    query2.equalTo( "tokenId", tokenIdTransfer);

    const object2 = await query.first({ useMasterKey: true });

    object2.set('forSale', false);

    object2.set('minimumBid', 0 );

    object2.set('buyNowPrice', 0 );

    object2.set('ownerAddress', toAddress );

    object2.set('ownerAddress', toAddress );

    object2.save(null, {useMasterKey: true});

  }

    logger.info("finish process");

});

you are using a moralis managed server or a self hosted server?

can you add some debugging with longer.info("message here")?

can you add a logger.info before this line to see if it gets here?

The logs are not executed, the detail is that the afterSave is not executed, and it should be noted that I am placing it in a table that creates moralis by default, but this is if I place it in the default table of moralis, if I execute it with another table that you have created is executed but all the fields are empty

did you also try with a beforeSave?

Yes I have tried but I have no success if I put beforeSave the table is not stored, because nothing is executed inside

what do you mean with the table is not stored?

does it get to execute that beforeSave?

I try to tell the class to be updated it doesn’t do it doesn’t run, it doesn’t update the table when doing .save(), yes And if I put beforeSAve in my class, the table is not saved because the internal function is not executed. If I put afterSave, which really should be afterSAve, nothing happens, the fields are empty. If I change to another class one of default moralis called PolygonNFTTransfers nothing happens nor is the afterSave executed

I am trying to update the existing table with an afterSave

I assume that you are using a moralis managed server now, for historical sync afterSave and beforeSave will not be called because of the way the data is added in the database

for real time sync they should work, before trying to update a table, does beforeSave of afterSave call?

can you use a beforeSave or afterSave only with a line that has loger.info?

If I leave only the log, if it enters the function, if I’m using a server managed by Moralis, so that everything works, I migrate it to my own server.

you can see that line in logs or not? I’m not sure by this answer

if i just run

Moralis.Cloud.afterSave("PolygonNFTTransfers", async (request) => {

logger. info("finish process");


});

returns the finish process, but if I execute

Moralis.Cloud.afterSave("PolygonNFTTransfers", async (request) => {

   const tokenIdTransfer = request.object.get("token_id");
   const toAddress = request.object.get("to_address");

   logger.info("tokenIdTransfer", tokenIdTransfer );
   logger.info("toAddress", toAddress );

   });

everything is empty

this is not the syntax for logger.info, it doesn’t work with 2 parameters, you have to send only one parameter

If you’re absolutely right, I’m going to debug a bit more and comment, thanks, I didn’t see it