Username gets changed randomly when put in the email or select a profile picture, the image doesnt load until I select another image or doesnot even work


<!DOCTYPE html>




      <script src="[email protected]/dist/web3.min.js"></script>

      <script src=""></script>




      <button id="btn-login">Connect Your MetaMask</button>

      <button id="btn-profile">Profile</button>

      <div id="userInfo">

          <h4>User Profile</h4>

          <input type="text" id="txtUsername" required placeholder="Enter Username">

          <input type="text" id="txtEmail" required placeholder="Enter Email Address">


        <img width = "50" height = "50" src="" id="imgAvatar" alt="">

        <label for="fileAvatar">Select Avatar</label>

        <input type="file" id="fileAvatar">

        <button id="btn-logout">Logout</button>

        <button id="btn-CloseUserInfo">Close</button>

        <button id="btn-SaveUserInfo">Save</button>


      <script type="text/javascript" src="./main.js"></script>





MoralisWeb3 = serverUrl = ""; //Server url from

MoralisWeb3 = appId = "wZsf07rYLfsPudepzTdjvrAsYfgWAQb0BFbnTFJI"; // Application id from

async function initializeApp() {


  await Moralis.start({ serverUrl, appId });

  await Moralis.enableWeb3();

  currentUser = Moralis.User.current();

  if (!currentUser){

       currentUser = await Moralis.Web3.authenticate();




initUser = async () => {

    if (await Moralis.User.current()) {



    }else {






login = async () => {

    try {

        await Moralis.Web3.authenticate();


    } catch (error) {




logout = async () => {

  await Moralis.User.logOut();




openUserInfo = async () => {

  user = await Moralis.User.current();

  if (user){

    const email = user.get('email');

    if (email) {

      userEmailField.value = email;

    } else {

      userEmailField.value = "";


  userUsernameField.value = user.get('username');

  const userAvatar = user.get('avatar');


    userAvatarImg.src = userAvatar.url();










saveUserInfo = async () => {

  user.set('email', userEmailField.value);

  user.set('username', userUsernameField.value);

  if (userAvatarFile.files.length > 0) {

    const avatar = new Moralis.File("avatar.jpg", userAvatarFile.files[0]);

    user.set('avatar', avatar);



  alert('Changes saved successfully!');



hideElement = (element) => = "none";

showElement = (element) => = "block";

const userConnectButton = document.getElementById("btn-login");

userConnectButton.onclick = login;

const userProfileButton = document.getElementById("userInfo");

userProfileButton.onclick = openUserInfo;

const userInfo = document.getElementById("userInfo");

const userUsernameField = document.getElementById("txtUsername");

const userEmailField = document.getElementById("txtEmail");

const userAvatarImg = document.getElementById("imgAvatar");

const userAvatarFile = document.getElementById("fileAvatar");

document.getElementById("btn-CloseUserInfo").onclick = () => hideElement(userInfo);

document.getElementById("btn-logout").onclick = logout;

document.getElementById("btn-SaveUserInfo").onclick = saveUserInfo;



here you may need an intermediate step to save that avatar, like

x = await
user.set('avatar', x)

not working and the username is still getting changed to something random when I click anywhere on the page

what you mean by:

like anywhere anywhere, or when clicking on some buttons?

I tested and username works fine for me, it looks random, but it is not random. that is the username that was generated.

when i click anywhere anywhere

can you send me that code which is working fine for you

I just copied your code, and added it in
It doesn’t work perfectly, but the username part looks fine.
I noticed that it tries to authenticate automatically on page load.

yeah, i know about the authentication problem, will fix it later

if you look in your server dashboard in User table, you will see the usernames that are saved in DB, and they look random there too, but that is the username that is generated automatically on first authentication.

can you send me the link on codesandbox of the code because the UI of the website is a bit confusing.

I created a new one now:
you will have to visit:

Thank You For Your Help Bro.

There is still one problem, now the username is getting changed from the user’s input to the random one again and when you click on Choose File or click in the input box for the Email Address the username changes from “Example” to random again, I get i that the server will receive the random generated code because it is personalized to the wallet’s id.

I understand now what you say, if I write something in that user field, and then click on next field, it is like somehow a function is called and resets all the data. If I hit tab and don’t click on next field then it works fine.

yes that is what I was trying to tell you

it looks like openUserInfo function is called every time you click on that ‘Enter Email Address’ input.

this may be the problem:

const userProfileButton = document.getElementById("userInfo");

userProfileButton.onclick = openUserInfo;

it means that every time you click on userInfo it runs that function :slight_smile:

so the fix to this problem is?

use the id of a button there instead of the id of a div, that (“userInfo”)

1 Like
<div id="userInfo">

          <h4>User Profile</h4>

          <input type="text" id="txtUsername" required placeholder="Enter Username">

          <input type="text" id="txtEmail" required placeholder="Enter Email Address">


The div and the id are both merged together