Documente Academic
Documente Profesional
Documente Cultură
In this lab will show you how to access the Firebase Real-time Database. Not just accessing
but run CRUD (Create, Read, Update, Delete) operation for populating Firebase Real-time
Database.
What we build here just the Ionic 4, Angular 6 and Firebase CRUD app for Bulletin Board
which just create an information then show the list of information in the home page of the
app. There also details of information, edit and delete the bulletin board information.
The Following tools, frameworks, and modules are required for this lab:
Node.js
Ionic 4
Angular 6
Firebase NPM Module
Firebase/Google Account
Terminal or Node.js Command Line
IDE or Text Editor
Before moving to the steps, make sure you have installed the latest Node.js and Ionic 4. To
check it, type this command in the terminal or Node.js command line.
node -v
v8.11.1
npm -v
6.2.0
ionic -v
Ionic CLI PRO 4.0.2
cordova -v
7.1.0
That's the last version when this lab was written. For update Node.js and it's `npm` download
from Node.js https://nodejs.org and choose the recommended version. For Ionic 4, type this
command.
Lab Objectives
After completing this lab, you will be able to:
Setup Firebase
Create a New App
Install and Configure Firebase Module
Add List of Bulletin Board
Add Page for Create Bulletin Board Info
Add Page for Show Bulletin Board Info Detail
Add Page for Edit Bulletin Board Info
Lab Procedures
A. Setting up Firebase
Go to console.firebase.google.com and log in using your google account.
Click CREATE NEW PROJECT button, name it as you like and choose your
country. Finally, click CREATE PROJECT button.
You will see questions during the installation, just type `N` for now. Next, go to the
newly created app folder.
cd ./ionic-firebase-crud
ionic serve -l
Type `Y` if asking to install `@ionic/lab`. Now, the browser will open automatically
then you will see this Ionic 4 Lab page.
Next, register the Firebase module in the Ionic 4 / Angular 6 app by open and edit this
file `src/app.component.ts` then add this import.
const config = {
apiKey: 'YOUR_APIKEY',
authDomain: 'YOUR_AUTH_DOMAIN',
databaseURL: 'YOUR_DATABASE_URL',
projectId: 'YOUR_PROJECT_ID',
storageBucket: 'YOUR_STORAGE_BUCKET',
};
Add this line inside the `initializeApp` function for running the Firebase
configuration.
Declare variables before the constructor for hold bulletin board list and referencing
Firebase database.
infos = [];
ref = firebase.database().ref('infos/');
Add this Firebase function to listening any value changes from Firebase Database.
Add this constant function below the Class block for converting Firebase response to
an array.
snapshot.forEach(childSnapshot => {
let item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
edit(key) {
this.router.navigate(['/edit/'+key]);
}
async delete(key) {
const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Are you sure want to delete this info?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('cancel');
}
}, {
text: 'Okay',
handler: () => {
firebase.database().ref('infos/'+key).remove();
}
}
]
});
await alert.present();
}
Finally, open and edit `src/app/home/home.page.html` then replace all tags with
this.
<ion-header>
<ion-toolbar>
<ion-title>
Info List
</ion-title>
<ion-buttons slot="end">
<ion-button routerLink="/create"><ion-icon name="add-
circle"></ion-icon></ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
imports: [
CommonModule,
FormsModule,
IonicModule,
ReactiveFormsModule,
RouterModule.forChild(routes)
],
ref = firebase.database().ref('infos/');
infoForm: FormGroup;
Next, add the function to post the form data to the Firebase Real-time Database.
saveInfo() {
let newInfo = firebase.database().ref('infos/').push();
newInfo.set(this.infoForm.value);
this.router.navigate(['/detail/'+newInfo.key]);
}
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
</ion-buttons>
<ion-title>Create Info</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<form [formGroup]="infoForm">
<ion-item>
<ion-label position="floating">Info Title</ion-label>
<ion-input type="text"
formControlName="info_title"></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Info Description</ion-
label>
<ion-input type="text"
formControlName="info_description"></ion-input>
</ion-item>
info = {};
Load info data from Firebase Real-time Database to the body of the constructor.
firebase.database().ref('infos/'+this.route.snapshot.paramMap.g
et('key')).on('value', resp => {
this.info = snapshotToObject(resp);
});
}
Create this class for converting Firebase Real-time Database response to the Angular 6
object.
return item;
}
<ion-header>
<ion-content padding>
<ion-card>
<ion-card-content>
<ion-card-title>{{info.info_title}}</ion-card-title>
<p>{{info.info_description}}</p>
</ion-card-content>
</ion-card>
</ion-content>
Next, open and edit `src/app/edit.module.ts` then add or modify this import to
add `ReactiveFormsModule`.
imports: [
CommonModule,
FormsModule,
IonicModule,
ReactiveFormsModule,
RouterModule.forChild(routes)
],
ref = firebase.database().ref('infos/');
infoForm: FormGroup;
Create a function for load info data based on the key that gets from params.
getInfo(key) {
firebase.database().ref('infos/'+key).on('value', resp => {
let info = snapshotToObject(resp);
this.infoForm.controls['info_title'].setValue(info.info_title);
this.infoForm.controls['info_description'].setValue(info.info_d
escription);
});
}
Call that function after initializing the FormGroup to the body of the constructor.
Create this class for converting Firebase Real-time Database response to the Angular 6
object.
return item;
}
Add this function to update the data from the FormGroup to the Firebase Real-time
Database.
updateInfo() {
let newInfo =
firebase.database().ref('infos/'+this.route.snapshot.paramMap.g
et('key')).update(this.infoForm.value);
Finally, open and edit `src/app/edit/edit.page.html` then replace all HTML tags
with this.
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
</ion-buttons>
<ion-title>Edit Info</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<form [formGroup]="infoForm">
<ion-item>
<ion-label position="floating">Info Title</ion-label>
<ion-input type="text"
formControlName="info_title"></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Info Description</ion-
label>
<ion-input type="text"
formControlName="info_description"></ion-input>
</ion-item>
<ion-button shape="round" color="primary" expand="block"
[disabled]="!infoForm.valid"
(click)="updateInfo()">Update</ion-button>
</form>
</ion-content>
ionic serve -l
And here we go, just test all of the CRUD function of this Ionic 4, Angular 6 and
Firebase app.
That it's, the Ionic 4, Angular 6 and Firebase CRUD mobile apps. You can get the
working full source code in our lab files.