Rarible clone how to catch and show duplicated emails and usernames to the user?

Hi guys, I wanted to alert the user about the duplication error. How can I do it with moralis? It is showing me this error on the console in browser >

Uncaught (in promise) Error: Account already exists for this username.
    at handleError (moralis.js:22042)

and this >

moralis.js:21909 POST https://imgvpmtqkpng.moralis.io:2053/server/classes/_User/7vCpYk1eiAZWbpUae4b5f2rN 400 (Bad Request)

Here is save profile function code >

saveUserInfo = async() => {
    user.set('email', userEmail.value); //same changes have applied to the validate email function from user to functin 
    user.set('username', textUsrName.value);
    if (userAvatarFile.files.length > 0) {
        const avatar = new Moralis.File("avatarprfl.jpg", userAvatarFile.files[0]);
        user.set('avatar', avatar);
      }try {
          user.save();
      } catch (error) {
         window.alert("Ижилхэн нэртэй байна.") 
         return openUsrInfo();
      }
      await user.save();
      alert("Таны мэдээлэл амжилттай хадгалагдлаа");
      console.log("123",error)
      openUsrInfo();
}
1 Like

Hello @bouncyknighter

I’ve did debugging. Function saveUserInfo with a few changes works correctly(I will send you formatted code later).

It looks like you are trying to register user which already exists. Please share me the full code, so I will be able to check it.

Uncaught (in promise) Error: Account already exists for this username.
    at handleError (moralis.js:22042)
1 Like

The function to alerting the user about the email duplication is already preparing :slightly_smiling_face:

Try this and let me know how it works :slightly_smiling_face:

saveUserInfo = async () => {
      let user = Moralis.User.current(); // delete this if setted globaly
      if (userEmail.value !== user.get("email")){
        user.set('email', userEmail.value); //same changes have applied to the validate email function from user to functin 
      } else {
        alert("duplication email error")
      }
      user.set('username', textUsrName.value);
      if (userAvatarFile.files.length > 0) {
        const avatar = new Moralis.File("avatarprfl.jpg", userAvatarFile.files[0]);
        user.set('avatar', avatar);
      }
      try {
        user.save();
      } catch (error) {
        window.alert("Ижилхэн нэртэй байна.")
        console.log("123", error)
        return openUsrInfo();
      }
      await user.save();
      alert("Таны мэдээлэл амжилттай хадгалагдлаа");
      openUsrInfo();
    }
2 Likes

Alright thanks, i will let you know after i try the code.

1 Like

Okay turns out it doesnt work, I have made new code but it doesnt catch duplicated email or usernames yet… From backend moralis it gives the error to the console, I am not sure how to show it to the client.

saveUserInfo = async() => {
    if(userEmail.value.length == 0){
        window.alert("Та бүртгүүлэх email-ээ оруулна уу? ")
    }else if(textUsrName.value.length == 0){
        window.alert("Та өөрийн хэрэглэгчийн нэрийг оруулна уу? ")
    }
    user.set('email', userEmail.value); //same changes have applied to the validate email function from user to functin 
    user.set('username', textUsrName.value);
    if (userAvatarFile.files.length > 0) {
        const avatar = new Moralis.File("avatarprfl.jpg", userAvatarFile.files[0]);
        user.set('avatar', avatar);
      }
      await user.save();
      alert("Таны мэдээлэл амжилттай хадгалагдлаа");
      openUsrInfo();
}

okay guys, I have successfully changed the code, Now it shows the duplicate email address warning to the user. U need to use Try catch method.
Here is the complete code whoever wants to work on it! Good luck everyone!
if u did improve or change the code please share it with us thanks!

saveUserInfo = async() => {
    if(userEmail.value.length == 0){
        window.alert("Та бүртгүүлэх email-ээ оруулна уу? ")
        return
    }else if(textUsrName.value.length == 0){
        window.alert("Та өөрийн хэрэглэгчийн нэрийг оруулна уу? ")
        return
    }
    user.set('email', userEmail.value); //same changes have applied to the validate email function from user to functin 
    user.set('username', textUsrName.value);
    if (userAvatarFile.files.length > 0) {
        const avatar = new Moralis.File("avatarprfl.jpg", userAvatarFile.files[0]);
        user.set('avatar', avatar);
      }
      try {
          await user.save();
          alert("Таны мэдээлэл амжилттай хадгалагдлаа");
      }catch(err){
          alert("өөр хэрэглэгчийн  нэр болон эмайл сонгоно уу? ")
      } 
      
      openUsrInfo();
}
2 Likes

Thank you for sharing the fix! Really appreciate it from the entire community! :slight_smile:

Happy BUIDLing!

2 Likes