Sync weirdness: strange log & code not executed?

Hi, I have created one sync that watches for my custom mint event.

I have a beforeSave that watches for the new entry in the associated db, andā€¦ I get some weird results:

1- The log is really strange, the before save is called 3 times !?!

The log is bottom to top

** And another call with confirmed false !?! **
** and no ā€œbeforeSave calledā€, which is impossible !!! **

  1. 2022-07-19T18:45:28.095Z - confirmed: false

** What the ??? a second call with true ?! **
7. 2022-07-19T18:53:47.581Z - confirmed: true
8. 2022-07-19T18:53:47.580Z - beforeSave called

**Second call: normal, the transaction is confirmed **
14. 2022-07-19T18:53:47.295Z - confirmed: true
15. 2022-07-19T18:53:47.294Z - beforeSave called

First This is normal
16. 2022-07-19T18:45:28.095Z - confirmed: false
17. 2022-07-19T18:45:28.095Z - beforeSave called

2- it is supposed to modify an object in db, but it does not

 const medalId = request.object.get('medalId')
    const tokenId = request.object.get('tokenId')
    const to = request.object.get('to') 

    logger.info("medalid " + medalId)
    logger.info("tokenId " + tokenId)
    logger.info("to " + to)

// I checked the log, the values are coherent

    // We look for medaille

    const Medaille = Moralis.Object.extend("Medaille")
    let aquery = new Moralis.Query(Medaille);
    aquery.equalTo("objectId", medalId)
    const _result = await aquery.find({ useMasterKey: true })

    logger.info("length " + _result.length)
    // 1 is found, no issue !

    const medaille = _result[0]

    // we update the medal
    medaille.set("tokenId", tokenId)
    medaille.set("minted", true)
    medaille.set("to", to)
    try {
        medaille.fetch({ useMasterKey: true })

But the medal db is not updated !
Is there something obvious I missed !?

Cheers

what is that .fetch supposed to do?

Hi;

Medaille.fetch() updates the state of medaille in the db, isnā€™t it ?
(Medaille is updated using the parameters passed through the event)

I usually use this type of syntax: .save(null, { useMasterKey: true })

I have the same result: no error returned from the try/catch around the fetch/save, and the object is not updatedā€¦

The goal of this code is to change the state of the medal object (being minted, minted) so thatā€™s why it is in a beforeSave function

you also have to use await

No change,

The query works fine, I find the medal and the data are coherent, but the fetch or save does nothing.
Is there some specific things to do due to the fact that it is in a cloudfunction/beforeSave ?
Is there some potential right tricks ?

I checked the medaille.set, using a medaille.get just after, it works fine ! Only the save is not functional

Cheers

how do you make the save now?
can you test that save part in a different cloud function to see if it works as expected?

I save this way

  try {
       await medaille.save(null,{ useMasterKey: true })
    } catch (error)
    { logger.error(error)}

And you wonā€™t believe it:
it works in a regular cloud function
and it does nothing in a beforeSaveā€¦

you are trying to save current object that corresponds to beforeSave?

I don"t understand your question,
In the beforreSave, I query a specific medal, with a given objectId (passed by the event) and then change some values on the medal, and save

The only link between medal and the beforeSave is the objectId passed by the function

The same code works fine outside of a beforeSave

ok, if you were trying to save same object that corresponds to the beforeSave then it would enter in an infinite loop

can you try with afterSave to see if it works?

Same result !

I also tried to open the writting rights to everyoneā€¦ same result.

Other strange thing:

the db with the same name as my Sync contains some entry, and only 3 are ā€œconfirmedā€, but they all succeeded - actually, only the one AFTER I moved from beforeSave to afterSave have a ā€œconfirmedā€ status

After the fetch/save, I have a log, and it is executedā€¦ so ā€¦ I donā€™t get whatā€™s happening

what is the code that you use now?

Moralis.Cloud.afterSave("trophyMinted", async (request) => {
    const confirmed = request.object.get('confirmed')  
    const logger = Moralis.Cloud.getLogger()
    
    logger.info("beforeSave called")
    logger.info("confirmed: "+confirmed )

    // If not confirmed we skip writting into the db
    if (!confirmed) return
  

    
    const medalId = request.object.get('medalId')
    const tokenId = request.object.get('tokenId')
    const to = request.object.get('to') 

   // We check the data are correct
    logger.info("medalid " + medalId)
    logger.info("tokenId " + tokenId)
    logger.info("to " + to)
    // And they are !

    // -------------- updating Medal ----------
    // On cherche la medaille correspondante
    const Medaille = Moralis.Object.extend("Medaille")
    let aquery = new Moralis.Query(Medaille);
    aquery.equalTo("objectId", medalId)
    const _result = await aquery.find({ useMasterKey: true })

    logger.info("length " + _result.length)
    if (_result.length === 0) {
        logger.error("Medaille not found " + medalId)
        return
    }
    const medaille = _result[0]

    // We update the medal data
    medaille.set("tokenId", tokenId)
    medaille.set("minted", true)
    medaille.set("to", to)
    
    // We check Medal is correctly updated, and it is !
    logger.info("Medaille controle TokenId:" + medaille.get("tokenId"))
    logger.info("Medaille controle to:" + medaille.get("to"))
    logger.info("Medaille controle minted:" + medaille.get("minted"))
    

    try {
       await medaille.save(null,{ useMasterKey: true })
    } catch (error)
    { logger.error(error)}
    logger.info("After fetch")

More weirdness:

My regular cloud function correctly updates Medal objects.
Soā€¦ i tried to call

await Moralis.Cloud.run(ā€œtestā€, params);

from afterSaveā€¦

And the medal is not updated !

If I call test from a browser, with a regular user, it worksā€¦ from afterSave, it does not !

Strange, I didnā€™t see this before. You have to call it with await to make it work

there is an ā€œawaitā€ā€¦

try {
await medaille.save(null,{ useMasterKey: true })

I mean when you called the cloud function separately

there is an await :frowning: