[SOLVED] My cloud function empty, but have errors in log

  1. i has async events to class EvensItemAdded -> it async ok;
  2. i have function beforeSave and aftersave "EvensItemAdded -> Ă­t not work;
  3. i try clean code in cloud -> restart and update server buy button’s moralis -> have error in Error logs

you can expect to see those error when server restarts, those are errors that will show when the server restarts

1 Like

Thanks you!
do you know, why cloud function dont work when class event async ?

that could be a different issue, can you share the server url?

what is the function that you expect to run on event sync?

server url: https://dmgsm8crkvxl.usemoralis.com:2083

i have event sync “EventsItemAdded”. And a cloud function afterSave “EventsItemAdded”, it insert or update to class “ItemsForSale”.
if auto sync it not work, but manual adjustment at class “EventsItemAdded” it works normally
My code:

Moralis.Cloud.afterSave("EventsItemAdded", async (request) => {
  const confirmed = request.object.get("confirmed");
  if (confirmed) {
    let query = new Moralis.Query("ItemsForSale");
    query.equalTo("tokenId", +request.object.get("tokenId"));
    query.equalTo("nftAddress", request.object.get("tokenAddress"));
    let itemForSale = await query.first({useMasterKey: true});
    
    try{
      if(!itemForSale){
        const update = {
          "uid": +request.object.get("uid"),
          "isSold": false,
          "askingPrice": +request.object.get("askingPrice"),
          "bidStart": +request.object.get("bidStart"),
          "bidEnd": +request.object.get("bidEnd"),
          "tokenId": +request.object.get("tokenId"),
          "marketAddress": request.object.get("address"),
          "nftAddress": request.object.get("tokenAddress"),
          
        };
        await Moralis.bulkWrite("ItemsForSale", [{
          update
        }]);
      }else{
        itemForSale.set("uid", +request.object.get("uid"));
        itemForSale.set("isSold", false);
        itemForSale.set("askingPrice", +request.object.get("askingPrice"));
        itemForSale.set("bidStart", +request.object.get("bidStart"));
        itemForSale.set("bidEnd", +request.object.get("bidEnd"));
        itemForSale.set("tokenId", +request.object.get("tokenId"));
        itemForSale.set("marketAddress", request.object.get("address"));
        itemForSale.set("nftAddress", request.object.get("tokenAddress"));
        
        await itemForSale.save();
      }   
    }catch(error){
      logger.error("EventItemsAdded got an error " + error.code + " : " + error.message + JSON.stringify([request.object.get("tokenId"),request.object.get("tokenAddress")]));
    }
  }else{
    logger.error("EventItemsAdded not confirmed ");
  }
});

add a logger.info on the first line to see if it is called or not when the event sync takes place

this function will not be run for historical event sync, only for real time event sync

1 Like

It worked :slight_smile: . Thanks @cryptokid

1 Like