Moralis.object.save() not working! (also relevant to the whalewatch youtube tutorial)

Hi, I’m trying to create a buy alert, working off of the whalewatch youtube tutorial.

The issue I’m facing is that I am not able to save new objects. I’ve googled and searched this forum everywhere for days, looking for some past similar questions asked and answered. I’ve exhausted all of them and still not getting anywhere. I’ve tried all different methods, by simply trying to do request.object.set for all subclass columns, but did not work. So I decided to create a new class using extend but I’m still not getting any new classes being saved nor new subclasses. I’ve tried creating a new class manually, creating columns ahead of time as well, and still didn’t work.

Please HELP!!

I can’t tell what I’m doing wrong as I don’t see any error logs on the dashboard.

Moralis.Cloud.beforeSave(“LeMuNewInvAlertTestA”, async function (request) {

const account = request.object.get(“invAddr”);
const txHash = request.object.get(“transaction_hash”);
const NewBuy = Moralis.Object.extend(account);
const newBuy = new NewBuy();

//Get token price on PancakeSwap v2 BSC
const options = {
address: “0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c”, //WBNB
chain: “bsc”,
exchange: “PancakeSwapv2”,
};
const priceData = await Moralis.Web3API.token.getTokenPrice(options);
const bnbPrice = priceData.usdprice;

const invAmtNoDec = request.object.get(“invAmount_decimal”) / Math.pow(10, 18);
const amtBonds = request.object.get(“amtBonds”);
const bondValNoDec = request.object.get(“bondValue_decimal”) / Math.pow(10, 18);
const leXCValNoDec = request.object.get(“leXCValue_decimal”) / Math.pow(10, 18);
const prtnrshpInt = request.object.get(“prtnrshpInt_decimal”) / 100;
const TVLNoDec = request.object.get(“TVL_decimal”) / Math.pow(10, 18);

const invAmtInD = invAmtNoDec * bnbPrice;
const bondValInD = bondValNoDec * bnbPrice;
const leXCValInD = leXCValNoDec * bnbPrice;
const TVLInD = TVLNoDec * bnbPrice;

newBuy.set(“accountAddress”, account);
newBuy.set(“txHash”, txHash);

newBuy.set(“invAmount”, invAmtNoDec);
newBuy.set(“amtBonds”, amtBonds);
newBuy.set(“bondValue”, bondValNoDec);
newBuy.set(“leXCValue”, leXCValNoDec);
newBuy.set(“prtnrshpInt”, prtnrshpInt);
newBuy.set(“TVL”, TVLNoDec);

newBuy.set(“invAmtInD”, invAmtInD);
newBuy.set(“bondValInD”, bondValInD);
newBuy.set(“leXCValInD”, leXCValInD);
newBuy.set(“TVLInD”, TVLInD);

Moralis.Object.registerSubclass(account, NewBuy);

await newBuy.save(null, { useMasterKey: true }).then(
(newBuy) => {
// Execute any logic that should take place after the object is saved.
//alert("New buy recorded with Account address: " + newBuy.accountAddress);
},
(error) => {
// Execute any logic that should take place if the save fails.
// error is a Moralis.Error with an error code and message.
//alert("Failed to record a new buy, with error code: " + error.message);
},
);

});

Moralis.Cloud.afterSave(“LeMuNewInvAlertTestA”, async function (request) {
const contrAddr = ‘CONTRACTADDRESSXXXXXX’;

const confirmed = request.object.get(“confirmed”);

if (confirmed) {

// if contract address of tx == watched contract address
if (request.object.get("address") == contrAddr) {

  const account = request.object.get("invAddr");
  const NewBuy = Moralis.Object.extend({ className: account, }, { useMasterKey: true, });
  const buyData = new Moralis.Query(NewBuy, { useMasterKey: true, });
  buyData.get(txHash);
  sendTelegramAlert(buyData);
};

}
});

@iS.StudioWorks
Its amazing to see the amount of hard work you put in there.

I suggest you break it down to a single object to save first and then try saving multiple object.
Let me know if that works??, meanwhile i will post the formatted code and try you code snippet.

Did you check if these aftersave and beforesave functions are triggered?

Did you save any new data in LeMuNewInvAlertTestA class?

You can check these logs for any cloud functions related errors.
image

Formatted code, you can use to formate the code snippet like below

Your code goes here

I was trying to do this but couldn’t figure it out lol thanks

yeah I check those logs and I see that the api for the token price is triggered but not the beforesave or aftersave (and no errors). are they supposed to show up as triggered if they were? And if they are not being triggered, why wouldn’t they, when a new subclass in LeMuNewInvAlertTestA got created?

If they have triggered you could find the data in logs. Can you try to save one more row in LeMuNewInvAlertTestA and see if you can find anything under logs.

If the aftersave and beforesave are working, then try adding a logger to check which part of the code in aftersave and beforesave is not working.