Trouble creating row in new table in beforeSave

I’m trying to create an entry in a new table i created called notifications except my code isn’t doing anything.

Moralis.Cloud.beforeSave("BscItemOffers", async (request) => {
     const logger = Moralis.Cloud.getLogger();
   
    const query = new Moralis.Query("BscNFTOwners");
    query.equalTo("token_id", request.object.get('tokenId'));
    query.equalTo("token_address", request.object.get('tokenAddress'));
    const object = await query.first();
    if (object){
        const buyer = request.object.buyer;
        const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", buyer);
      const userObject = await userQuery.first({useMasterKey:true});
      if (userObject){
          request.object.set('user', userObject);
      }
      if(request.object.get("expired") != true){
       request.object.set('expired', false);
      }
      request.object.set('item', object);
      
      if(true){
        
      const Notifications = new Moralis.Object.extend("Notifications");
     const notifications = new Notifications();

      notifications.set("event", "Item Offer");
      notifications.set("userAddress", object.get("owner_of"));
      notifications.set("item", request.object);
      notifications.set("viewed", false);

      notifications.save();
      
      logger.info("notifi: "+ notifications);
      }
    }

  });

Hey @PeacanDuck

.save() is an async method. You need to use await.

It depends on CLP settings, but usually you need to use methods (find, first, save) with masterkey.

All save functions should be like: item.save(null, {useMasterKey:true})

query.first({useMasterKey:true})
query.find({useMasterKey:true})

Hey @Yomoo

im getting this error

Changed this

  const Notifications = new Moralis.Object.extend("Notifications");
     const notifications = new Notifications();

      notifications.set("event", "Item Offer");
      notifications.set("userAddress", object.get("owner_of"));
      notifications.set("item", request.object);
      notifications.set("viewed", false);

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

side note this works in js console but not in cloud code

const Notifications = new Parse.Object.extend("Notifications");
     const notifications = new Notifications();

      notifications.set("event", "Item Offer");
      notifications.set("userAddress", "0xfjbn3");
     // notifications.set("item", "item");
      notifications.set("viewed", false);

      await notifications.save();

Hi,
Maybe this helps:
https://docs.moralis.io/moralis-server/database/objects#updating-objects

// Create the object.
const GameScore = Moralis.Object.extend("GameScore");
const gameScore = new GameScore();

Hey @cryptokid that’s what I’ve been using as a reference but it doesn’t work in cloud code i get the error above

I wanted to say that it looks like you have const Notifications = new Parse.Object.extend("Notifications"); compared to const GameScore = Moralis.Object.extend("GameScore");, as in an extra new there.

1 Like

Thanks i removed new and its working now. Just need to get the request object to save in the item field now

1 Like