[SOLVED] Using moralis beforeSave with react

Hi, saw this solution on the forum:

Moralis.Cloud.beforeSave("Projects", async (request) => {
  const logger = Moralis.Cloud.getLogger();
  logger.info("beforeSave!");
  logger.info(request.object.get('title'));
  const query = new Moralis.Query("Projects");
  query.equalTo("title", request.object.get('title'));
  const queryResults = await query.find({useMasterKey:true});
  logger.info("queryResults length="+queryResults.length);
  if(queryResults.length > 0){
    throw 'You must choose an unique name for your project.';
  } 
  // do any additional beforeSave logic here
});

But don’t understand how such an entry would look with react-moralis library. Please help me

Hi, that code function is meant to be used inside your cloud code

Oh, okay, thanks.
So I should use a useMoralisSubscription instead?

If you are using react-moralis then yes, you can make use of useMoralisSubscription to detect when an object is created

1 Like

Hey @orus,

do you have any other issue or questions you would like to ask? :grinning_face_with_smiling_eyes:

If not, then I’ll be closing this thread. Feel free to open a new one in the future if you have any other inquiries.

Hi, nothing to ask. Close this thread please)

1 Like