How to Update and Delete data to Realtime Database ?



Use update() function for update data to firebase realtime database and use remove() function to delete data to firebase realtime database. Follow the step to update and delete the data for web to realtime database to firebase.

  1. Updata Data :
    update(ref(database, 'PARENT_NODE'), {CHILD_NODE : VALUE}) : 
    import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
    import { update, ref } from 'firebase/database';

    update(ref(database,'PARENT_NODE'),{CHILD_NODE: VALUE});

    * First argument : update ref take  PARENT_NODE as reference and update its CHILD_NODE with VALUE.

    * Second argument : It always takes an object.


    For example,

    Available data in realtime database :

    Data in realtime database
    We want to update mobileNumber so PARENT_NODE is 'users/userInfo' and as a object CHILD_NODE is 'mobileNumber' with its updated value '+1 1111111111'.

    So code for that is,

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

    update(ref(database, 'users/userInfo'), { mobileNumber: '+1 1111111111' });

    There is second way to write code for the same functionality,
    Take PARENT_NODE as 'users' and as a object CHILD_NODE is 'userInfo/mobileNumber' with its value '+1 1111111111'.
    import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
    import { update, ref } from 'firebase/database';

    update(ref(database, 'users'), { 'userInfo/mobileNumber': '+1 1111111111' });
    Updated data in realtime database

  2. update() return Promise so you can run code on fulfilled and handle errors using '.then()' or 'async/await'.

    '.then( )' : Code for that,
    update(ref(database, 'users'), { 'userInfo/mobileNumber': '+1 1111111111' })
      .then(() => {
        // Run code after successful update
      })
      .catch((error) => console.log(error));

  3. Delete Data :
    remove(ref(database, 'REFERENCE_PATH')) : 

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

    remove(ref(database, 'REFERENCE_PATH'));


REFERENCE_PATH is path location of 'node' whose data will remove.

For example,

We want to delete 'mobileNumber' field so for that REFERENCE_PATH is 'users/userInfo/mobileNumber'. So code for that is,
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { remove, ref } from 'firebase/database';

remove(ref(database, 'user/userInfo/mobileNumber'));

remove() return Promise so you can run code on fulfilled and handle errors using '.then()' or 'async/await'.

'.then( )' : Code for that is,
remove(ref(database, 'user/userInfo/mobileNumber'))
  .then(() => {
    // Run code after successful deletion
  })
  .catch((error) => console.log(error));

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

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

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

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.