How to Save Data to Realtime Database ?



Follow the steps to write data to Firebase Realtime Database for Web :

1. Save Data :

set (ref(database, 'PATH_TO_SAVE'), 'DATA') :

import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { set, ref } from 'firebase/database';

set(ref(database, 'PATH_TO_SAVE'), 'DATA');
  • "PATH_TO_SAVE" : Give path location where data will be saved.
  • "DATA" : This data will be saved at this location PATH_TO_SAVE [It can be String, Array, Object]
  • Data will be saved as JSON format.

    For example,

    If you set "PATH_TO_SAVE" : "user/mobileNumber" and "DATA" : "+1 0000000000".

    Then Data will be store as JSON like this, 

{
  "user": {
    "mobileNumber": "+1 0000000000"
  }
}

So for that you need to write this way,
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { set, ref } from 'firebase/database';

set(ref(database, 'user/mobileNumber'), '+1 0000000000');

Data will save on Realtime Database like this,
Realtime database JSON data



Lets take one more example and save object,
"PATH_TO_SAVE" : "users/userInfo" ,
"DATA" : { name : 'Robert Downey Jr', mobileNumber : '+1 0000000000' }
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { set, ref } from 'firebase/database';

set(ref(database, 'users/userInfo'), {
  name: 'Robert Downey Jr',
  mobileNumber: '+1 0000000000',
});

Data will save on Realtime Database like this,
Realtime Database saved JSON Data

2. Update using Set( ):

  • If you add PATH_TO_SAVE that already exist in database then it overwrite with new data.
  • So you can update data using this way. Simply add path where you want update and paas updated data. 

3. Delete using Set( ):

  • Add PATH_TO_SAVE that want to delete and use DATA as "null".
set(ref(database, 'PATH_TO_SAVE'), null);


Related Post
Read data
How to Read Data from Realtime Database ?
How to Read Data from Realtime Database ?

1.Setup and Initialize it, 2.You can read data using 'get()' and 'onValue()' method

21 October 2024

Write data
How to Save Data to Realtime Database ?
How to Save Data to Realtime Database ?

1. Setup Firebase SDK and Initialize it 2. Save data : use "set (ref(database, 'PATH_TO_SAVE'), 'DATA')" method

21 October 2024

Setup and Initialize
Setup and Initialize Realtime Database
Setup and Initialize Realtime Database

1.Open realtime database dashboard, 2.Click Create Database, 3.Select location and Security rules, 4.Setup Firebase SDK and Initialize it

21 October 2024

Update and Delete data
How to Update and Delete data to Realtime Database ?
How to Update and Delete data to Realtime Database ?

1. Setup Firebase SDK and Initialize it, 2.Update data using 'update()' method 3.Delete data using 'remove()'method

21 October 2024

Company
Explore

Copyright © 2024, All rights reserved by Firebase Master

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Allow", you consent to our use of cookies. Here is our Privacy Policy.