1

I ran into this a while back and it was doing my head in.

Could it be that a ‘Moralis.Cloud.beforeSave’ in Cloud Functions is not triggered when adding tables through a sync job (plugin)?

When I run the beforeSave just using the web3 UI it works. When I delete all the tables and manually run the Sync job to create new tables, beforeSave did not run to add the additional user and token fields:

// link sale and item data Polygon
Moralis.Cloud.beforeSave("ItemsForSalePolygon", async (request) => {
  const query = new Moralis.Query("PolygonNFTOwners")
  query.equalTo("token_address", request.object.get('tokenAddress'))
  query.equalTo("token_id", request.object.get('tokenId'))
  const object = await query.first()
  if (object){
      const owner = object.attributes.owner_of
    const userQuery = new Moralis.Query(Moralis.User)
      userQuery.equalTo("accounts", owner)
    const userObject = await userQuery.first({useMasterKey:true})
    if (userObject){
        request.object.set('user', userObject);
    }
    request.object.set('token', object);
  }
})

Or is the sync job cached somehow?

1

For performance reasons, the historical sync uses bulk insert. A side effect of this optimization is that triggers are not fired for those inserts. This explains the behaviour you observed where the triggers fire on real-time inserts but not when syncing historical data.

You can create a Jobs or cloud function and run it manually to take care of any initialization that needs to occur after the initial historical sync

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.