Why is my function not writing the data onto my firebase realtime database

Firebase createUserWithEmailAndPassword Works but set() to Realtime Database Fails
I have a Firebase project where I need to register a user using Firebase Authentication, then immediately write the user’s data to the Firebase Realtime Database.

Here’s the structure of my registration function:


export function registerbtn(register, emailinput, passwordinput) {
    register.preventDefault();
    console.log(passwordinput, emailinput);
    const email = emailinput.value;
    const password = passwordinput.value;

    createUserWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            // Signed up
            const user = userCredential.user;
            writeUserData(user);  // Writing user data to Firebase DB
            const provider = "local";
            getUserDetails(user);
            saveUserdetails(user, provider);

            window.alert(email + password);
            window.location.href = accountredirect;
            return user;

        })
        .catch((error) => {
            const errorCode = error.code;
            const errorMessage = error.message;
            console.log(errorMessage + errorCode);
            document.getElementById('error-text').innerHTML = errorCode;
        });
}

I also have a writeUserData function that writes the newly registered user’s data to Firebase Realtime Database:


function writeUserData(user) {
    const userId = user.uid;
    const info = "value";

    // Update the user's data in the database
    const db = getDatabase();
    set(ref(db, 'users/' + userId), {
        username: info,
        email: info,
        profile_picture: info
    })
    .then(() => {
        console.log("User data updated successfully!");
    })
    .catch((error) => {
        console.error("Error updating user data: ", error);
    });
}

The Issue:
Everything inside the registerbtn function works fine, except for the writeUserData function. It doesn’t write to the database, and the console logs show no errors related to Firebase database. However, if I call the writeUserData function separately or outside of the registration process, it works perfectly and writes the data to Firebase.

I’ve tried putting the writeUserData function inside and outside the .then() block, and even exporting/importing it, but the issue persists.

My Firebase Configuration:


import {
    initializeApp
} from "https://www.gstatic.com/firebasejs/10.13.0/firebase-app.js";


import {
    getAuth, signOut, signInWithEmailAndPassword, signInWithPopup, GoogleAuthProvider, createUserWithEmailAndPassword, onAuthStateChanged
} from "https://www.gstatic.com/firebasejs/10.13.0/firebase-auth.js";


import {
    getAnalytics
} from "https://www.gstatic.com/firebasejs/10.13.0/firebase-analytics.js";


import {
    getDatabase, onValue, child, ref, set, get
} from "https://www.gstatic.com/firebasejs/10.13.0/firebase-database.js";


// TODO: Add SDKs for Firebase products that you want to use

// https://firebase.google.com/docs/web/setup#available-libraries


// Your web app's Firebase configuration

// For Firebase JS SDK v7.20.0 and later, measurementId is optional

const firebaseConfig = {

    apiKey: "AIza**************",

    authDomain: "wespot-website.firebaseapp.com",

    databaseURL: "https://wespot-website-default-rtdb.europe-west1.firebasedatabase.app",

    projectId: "wespot-website",

    storageBucket: "wespot-website.appspot.com",

    messagingSenderId: "864768589048",

    appId: "1:864768589048:web:f76790c9abfa6b2e594f0d",

    measurementId: "G-0ZD710W4YE"

};


// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const database = getDatabase();
const auth = getAuth();
export const provider = new GoogleAuthProvider();
const accountredirect = "../index.html";

Steps I’ve Tried:
Logging the user object: To ensure the user object is populated, I added console logs, and it shows the correct user UID.

Firebase Rules: I’ve checked my Firebase Realtime Database rules and made sure they allow authenticated users to write.

Firebase Authentication works fine and the user is successfully created.
Database writes only fail when the writeUserData function is called right after the user registration.
There are no errors in the console, making it hard to trace the issue.
Question:
Why is writeUserData(user) failing when called after createUserWithEmailAndPassword but works perfectly fine when called separately? Could it be a timing issue, or am I missing something else?

Any help or insight would be appreciated!