title: i am trying to add user information to firebase createuserwithemailandpassword
body: <p>Whenever I try to add the user data, the wrong uid is being applied to the user data, it is applying the uid from the previous account created even after i have deleted the account from the database.</p>
  <p>I read that i must create a child node to add the remaining user credentials which is what I am trying to do. But the current uid from the recieved promise is not being sent to the user database. please help.
  <a href="https://i.stack.imgur.com/4ltD4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4ltD4.png" alt="enter image description here" /></a>
  second image
  <a href="https://i.stack.imgur.com/PzuaJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PzuaJ.png" alt="enter image description here" /></a></p>
  <pre><code>import React, { useState } from 'react'
  import {v4 as uuid} from 'uuid'
  import {db} from '../Features/util/firebase'
  import {set,ref} from 'firebase/database'
  import { getAuth, createUserWithEmailAndPassword } from &quot;firebase/auth&quot;;
  
  
  function Register() {
    const [fname, setFname]=useState('');
    const [lname, setLname]=useState('');
    const [phone, setPhone]=useState('');
    const [email, setEmail]=useState('');
    const [address, setAddress]=useState('');
    const [username, setUsername]=useState('');
    const [password, setPassword]=useState('');
    const [uid, setUid]= useState('');
  
    const createUser=(e)=&gt;{
      e.preventDefault();    
  
      const auth = getAuth();
  
      createUserWithEmailAndPassword(auth,email,password)
      .then((userCredential)=&gt;{
        console.log(&quot;signed in&quot;)
        const user = userCredential.user;
        console.log(user.uid);
        setUid(userCredential.user.uid)
      })
      .then(()=&gt;
      set(ref(db,'users/') , {
       fname:fname,
       lname:lname,
       phone:phone,
       email:email,
       address:address,
       username:username,
       userId:uid,
     }))
      .catch((error)=&gt;{
        const errorCode = error.code;
        const errorMessage = error.message;
        clearFields();
      });
  
      function clearFields (){
        setAddress('');
        setEmail('');
        setFname('');
        setLname('');
        setPassword('');
        setPhone('');
        setUid('');
        setUsername('');
      }       
    }   
    return (
      &lt;div&gt;
          &lt;form onSubmit={createUser}&gt;
              &lt;label &gt;First Name: &lt;/label&gt;
              &lt;input type='text' value={fname} onChange={(e) =&gt; setFname(e.target.value)} required/&gt;&lt;br/&gt;
  
              &lt;label &gt;Last Name: &lt;/label&gt;
              &lt;input type='text' value={lname} onChange={(e) =&gt; setLname(e.target.value)} required &gt;&lt;/input&gt;&lt;br&gt;&lt;/br&gt;
  
              &lt;label &gt; Phone No.: &lt;/label&gt;
              &lt;input type='tel' value={phone} onChange={(e) =&gt; setPhone(e.target.value)} required&gt;&lt;/input&gt;&lt;br&gt;&lt;/br&gt;
  
              &lt;label &gt;e-Mail: &lt;/label&gt;
              &lt;input type='email' value={email} onChange={(e) =&gt; setEmail(e.target.value)} required&gt;&lt;/input&gt;&lt;br&gt;&lt;/br&gt;
  
              &lt;label &gt;Address: &lt;/label&gt;
              &lt;input type='text' value={address} onChange={(e) =&gt; setAddress(e.target.value)} required&gt;&lt;/input&gt;&lt;br&gt;&lt;/br&gt;
  
              &lt;label &gt;Username: &lt;/label&gt;
              &lt;input type='text' value={username} onChange={(e) =&gt; setUsername(e.target.value)} required&gt;&lt;/input&gt;&lt;br/&gt;
  
              &lt;label &gt;Password: &lt;/label&gt;
              &lt;input type='password' value={password} onChange={(e) =&gt; setPassword(e.target.value)} required&gt;&lt;/input&gt;
  
              &lt;input type='submit'&gt;&lt;/input&gt;
          &lt;/form&gt;
      &lt;/div&gt;
    )
  }
  
  export default Register
  </code></pre>