Error handler service call

Hi,

 const data = new FormData(event.target);
    const query = new Moralis.Query("UserInfo");
    query.equalTo("userId", user);
    const result = await query.first({ useMasterKey: true });
    result.set("username", data.get("username").trim());
    result.save().then(
      (res) => {
        toast.success("Profile successfully updated!", options);
        navigate("/my-profile");
        setLoading(false);
      },
      (error) => {
        toast.error("Profile update unsuccessful! " + error.message, options);
        setLoading(false);
      }

If username is not unique in the before save trigger, I return an error and although the code enters the error section, it performs the save operation. What can i do to dont save if it returns error?

You could try to see if the user already exists with a query.

I don’t know why it will save when it returns an error