How to Save Data to Realtime Database ?
Follow the steps to write data to Firebase Realtime Database for Web :
- Setup Firebase SDK and Initialize Realtime Database.
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"
}
}
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { set, ref } from 'firebase/database';
set(ref(database, 'user/mobileNumber'), '+1 0000000000');
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',
});
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
Write data
Update and Delete data
Read data
Explore
Copyright © 2024, All rights reserved by Firebase Master