DEV Community

youssefdev98
youssefdev98

Posted on

How to save data in Firebase Realtime Database ? (Javascript)

Hey, i need to store some simple data on the firebase realtime database, i've connected my app to the firebase realtime database, and imported all the requirments in the javascript file, but whenever my code is done, i check the realtime database and find that nothing is stored, i've followed many tutorials on youtube, even the firebase tutorial on youtube, but the mentionned problem stays, the realtime database stays empty as if i didn't write any code. Here is the javascript code :

import { initializeApp } from 'firebase/app';
import { getDatabase, ref, set } from 'firebase/database';


const firebaseConfig  = initializeApp({
    apiKey: "AIzaSyChK5qg7AJi0_AZQAc2iWnvDVJjyJvz5iI",
    authDomain: "mycoolapp-4373a.firebaseapp.com",
    databaseURL: "https://mycoolapp-4373a-default-rtdb.firebaseio.com",
    projectId: "mycoolapp-4373a",
    storageBucket: "mycoolapp-4373a.appspot.com",
    messagingSenderId: "895684408479",
    appId: "1:895684408479:web:3341db270dcd768e085eaa"
});
const app = initializeApp(firebaseConfig);

function writeUserData(userId, username, email, message) {
    const db = getDatabase();
    const reference = ref(db, 'users/' + userId);

    set(reference, {
        username: username,
        email: email,
        message: message
    });
}

writeUserData("andrew","andrew__8","andrew@gmail.com", "hey..i'm andrew!!");

Enter fullscreen mode Exit fullscreen mode

Is there something i missed ?
Thank you in Advance !!!

Top comments (0)