Publish Ionic Web App to Firebase Hosting

Firebase hosting allows to host web apps, let’s see how can publish the Ionic app to Firebase hosting.

Following are the steps:

Generate production www web app folder from the Ionic app

Use following command in existing ionic project to generate production web app www folder.

ionic capacitor build browser --prod

This will generate a www folder, which we need to publish to the Firebase hosting.

Prepare Firebase Hosting project to publish the web app

Install Firebase CLI

Install Firebase CLI globally, if not already installed.

npm install -g firebase-tools

Login to Firebase, through Firebase CLI

Login to Firebase through firebase CLI, using following command. This will open a login screen in browser window. Login there.

firebase login

Create Firebase hosting project

Create new directory for firebase hosting project

mkdir hosting

Switch to directory

cd hosting

Initialize Firebase hosting project

Enter following to initialize firebase hosting project.

firebase init

Choose Yes, to initialize the Firebase project

Choose following hosting feature from listed options:

Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys

Next selected Use an existing project, because in my case I had an existing project.

Then select your project from the list.

Next question is

What do you want to use as your public directory? (public)

type www

Next Question

Configure as a single-page app (rewrite all urls to /index.html)? (y/N)

Choose Y

Next question

Set up automatic builds and deploys with GitHub? No

Enter, and firebase initialization will complete.

In the hosting folder one www folder will be created.

Copy www folder to Firebase hosting project

In the hosting folder one www folder will be created.

Replace this folder with the www folder generated above from Ionic production web app build .

Publish the web app

Finally, enter following command to publish the web app.

firebase deploy

Web app will be published to firebase hosting and you will be presented with the published web app url.

Congrats! We successfully published web app from ionic project to the Firebase hosting.

Conclusion

In this post we learned how to publish an Ionic web project to Firebase hosting.

First we generated the www folder from the Ionic app. Then We created and prepared a new project for publishing to Firebase hosting. In the Firebase hosting project we copied the www folder from the Ionic app and then finally published to Firebase hosting using firebase deploy

Firebase | Testing Firebase Cloud Function Locally with Cloud Function Emulator

Firebase CLI provides a cloud function emulator which can be used to run the Firebase Cloud Function locally before deploying to production. Following are the types of functions which can be emulated.

  • HTTP functions
  • Callable functions
  • Background functions triggered from Authentication, Realtime Database, Cloud Firestore, and Pub/Sub.

We will test a simple http function which we created in the last post.

Creating a Cloud Function Using Firebase CLI and TypeScript.

Following are the steps we will follow:

Install or Update the Firebase CLI

Firebase emulator is included in Firebase CLI, so we need to install it, or update it to the latest version.

npm install -g firebase-tools

Setting Up Admin Credentials for Emulated Functions

If your cloud function requires interaction with Google API or Firebase API via the Firebase Admin SDK then you may need to setup the admin credentials.

Go to the Service Accounts in Google Cloud Platform, under your project.

Select the service account row with Name “App Engine default service account“, and click on Actions button at the right end.

Select Manage Keys.

Click on Add Key drop down button.

Select Create new key.

This will open a modal window.

Choose JSON, from the available key types. This will generate and download the key in json file with key details.

JSON key file will look like following, values are omitted here.

{
  "type": "",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
}

Set Google Application Credentials to the JSON file path.

This is required to authenticate to Google API or Firebase API from Firebase Admin SDK.

Execute following from project’s root directory.

set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json

Run the Firebase Emulator

Now we are ready to start the emulator.

Start the emulator by following command

firebase emulators:start

Emulator will be started and will provide you the function URL.

Call Function URL

Click on provide function URL, and you will receive the response from your function.

Great! We are able to run function locally.

Firebase Emulator UI

You will also be presented with Emulator UI URL.

Open the emulator UI URL in the browser.

In the Emulator screen you will be able to see all the different types of emulators.

You can navigate to Logs, to see any logging by function.

Modify and Test Local Cloud Function

Now let’s update the function.

Change the function response message.

Build the function again, and execute the following from functions folder.

npm run build

Reload the function URL provide by the running emulator and you will see updated response from local function.

When you are happy, you can deploy the function to Firebase as explained in this post.

Firebase | Creating a Cloud Function Using Firebase CLI and TypeScript

Conclusion

The Firebase emulator, which is included in the Firebase CLI, let’s us test the function locally before deploying to the cloud.

In this post we started with installing Firebase CLI and then we setup the Google Account Credential. Google Account Credential requires a JSON file with private key, which was generated in the Google Cloud Platform. Then we started the Firebase emulator and browsed the local function URL and received response from the local function.

Hope you liked this, please share your feedback or any query.

Firebase | Creating a Cloud Function Using Firebase CLI and TypeScript

Overview

Firebase cloud functions let’s you run a piece of code in cloud without managing the servers. This is quite helpful when you just want to manage your code and not to worry about the servers executing the code. This pattern is also known as serverless architecture.

These cloud functions can be triggered by multiple events such as an http request, scheduled time or in response to changes in Realtime Database and perform the intended job.

In this post we will see how we can create a simple Firebase cloud function using Firebase CLI and then deploy it to Firebase. We will use TypeScript to write the function.

Steps for creating the Cloud Function

Let’s create and deploy a Firebase cloud function.

Installing Firebase CLI

Firebase cloud functions are created and deployed using Firebase CLI, so let’s install the Firebase CLI globally. Type following command on command prompt.

npm install -g firebase-tools

Login to Firebase CLI

We need to authenticate to Firebase to create and deploy cloud functions, authenticate to Firebase using following command.

firebase login

This will open a browser window to authenticate.

If you are logged in with another account then you can logout first using following.

firebase logout

Choose the account to login.

Allow, to grant permissions to Firebase CLI.

On Allow following success screen will be presented.

And in command prompt message like following will be logged.

Creating a Firebase Cloud Function Project

Create a directory for the project.

mkdir firebase-function-demo

Change to project directory.

cd firebase-function-demo

Open the directory with Visual Studio code or any other editor.

code .

Initialize Firebase functions project

firebase init functions

Accept the confirmation.

Choose the appropriate option for you. In my case I chose “Use an existing project“, because I have already created the Firebase project.

Next I chose the project from the presented list.

For this example we are going to use the TypeScript, so choose the TypeScript.

Choose Y if you want to use ESLint.

Select Y to install the dependencies.

Your project structure should appear like this so far.

Creating a Cloud Function

We will use the sample helloworld cloud function created by the Firebase CLI for this example.

Open selected index.ts.

index.ts contains commented sample function.

Uncomment the code, and save the file.

File contains one sample http request based cloud function. Which will log following.

"Hello logs!", {structuredData: true}

and return following response.

"Hello from Firebase!"

In the same file more functions can be added, for example we can add another scheduled function like following.

export const scheduledJob = functions.pubsub.schedule("every 5 minutes")
    .onRun(()=>{
      console.log("This will be run every 5 minutes.");
      return null;
    });

This scheduled function will run every five minutes,

cron expression can be added like following to define trigger time for scheduled functions.

export const  scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});

Deploying cloud function

Let’s deploy the sample code in index.ts to cloud.

Execute the following command on command prompt.

firebase deploy

On successful function deployment, function url will be provided.

Paste the URL in the browser.

and you will get the response like below from cloud function.

Great! Our cloud function is successfully deployed and responding to http request.

Navigate to Firebase project and select functions.

and you should be able to see your cloud function.

You can switch to Logs tab to see the logs of the cloud function.

Congratulations! We have just deployed our first simple cloud function.

Conclusion

In this post we learned how we can create a Firebase Cloud Function and deploy to Firebase. We started with how to install the Firebase CLI globally, and how to authenticate to it. Then we created a template project for cloud function using Firebase CLI and then deployed and tested the cloud function.

Hope you like this!

Please leave your feedback or any query you have.

Ionic 5 | Performing Firebase CRUD Operations Using Angular 12

In this blog post we will learn how to create a simple Notes application using Ionic 5 and Angular 12.

The Note application will be able to perform the read, create, update and delete operations, and Firestore database in the Firebase will be used as database.

The Note Application

We will cover following topics:

Setting up the Firebase Project to save notes

For our Notes app we are going to save notes in the Firebase, and for that we need to create a new Firebase project. If you have existing Firebase project you can also use that.

Creating a Firebase project

To create a Firebase project head over to Firebase console and click on Add Project.

Firebase Console

Give your project a name, it’s Notes in my case.

You can change your project’s unique name here, although that doesn’t matters.

Project name

Enable Google Analytics to your project (optionally).

Enable Google Analytics

If you have chosen to enable Google Analytics then select or create the account for the Google Analytics.

Account for Google Analytics

In the following I have selected a new Google Analytics account.

New Google Analytics account

Then finally click on create project.

This will take a few minutes to setup the new Firebase project.

Setting up Firebase project.
Firebase project setup done.

Click on continue.

and now we are inside the our brand new Firebase project.

Firebase project overview

Getting Firestore Database settings.

To store the Notes inside Firebase, we need some configuration settings from the Firebase project.

To get the settings, go to the Project settings.

Project settings option

Select the web app options from available platform options.

Web app option

Click on Register app.

App name

Copy the firebaseConfig from the next screen. We are going to need this in our Ionic app.

Then click on Continue to console at the bottom of the screen.

In the firebaseConfig one setting is missing in my case i.e. databaseURL.

The databaseURL specifies to which database data will be saved.

If this is missing in your case too, then add like following.

  var firebaseConfig = {
    apiKey: "AIzaSyDv6oqHgHBOV60BpXa0W_JSxKDOrYuT7_M",
    authDomain: "notes-3b077.firebaseapp.com",
    projectId: "notes-3b077",
    databaseURL: "https://notes-3b077.firebaseio.com/",
    storageBucket: "notes-3b077.appspot.com",
    messagingSenderId: "435123651850",
    appId: "1:435123651850:web:6f2e0f5f60decf031166f3",
    measurementId: "G-ZBDCPDDNFX"
  };

databaseUrl format is as following.

https://{your-app-unique-name}.firebaseio.com/

We have now created the Firebase project, and we have the required configurations.

Let’s now create our Ionic application for saving the notes.

Creating a new Ionic Application of Angular Type

At the time of writing the latest version available for Ionic is Ionic 5. and Angular has the latest version of 12.

To create an Ionic application we need the Ionic CLI.

Install Ionic CLI globally or update it to the latest version if you have already installed.

npm install -g @ionic/cli

Create a new Ionic application of Angular type with blank template.

ionic start ionic-firebase-crud blank --type=angular

Move to the ionic app you have just created.

cd ionic-firebase-crud

Configuring the Ionic application to support Firebase

Before we can start saving notes in the Firebase from Notes app, we need to add configurations.

Adding Firebase Configurations in the Application

Add the firebaseConfig that we copied earlier in the environment.ts file.

Firebase config in environment.ts

Installing Required Firebase packages in the Ionic Application

To work with Firebase we need to add few npm packages.

  • firebase
  • @angular/fire

Install the firebase and @angualr/fire using npm and save it to the package.json file.

npm install firebase @angular/fire --save

This will add the following highlighted dependencies in the package.json

  "dependencies": {
    "@angular/common": "~12.0.1",
    "@angular/core": "~12.0.1",
    "@angular/fire": "^6.1.5",
    "@angular/forms": "~12.0.1",
    "@angular/platform-browser": "~12.0.1",
    "@angular/platform-browser-dynamic": "~12.0.1",
    "@angular/router": "~12.0.1",
    "@ionic/angular": "^5.5.2",
    "firebase": "^8.6.8",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.4"
  },

Adding Firebase modules in the application

Import AngularFireModule and AngularFirestoreModule modules in the app.module.ts.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
// Firebase
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule} from '@angular/fire/firestore';
//Environment
import { environment } from 'src/environments/environment';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule, 
    IonicModule.forRoot(), 
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebaseConfig),
    AngularFirestoreModule
  ],
  providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
  bootstrap: [AppComponent],
})
export class AppModule {}

The configuration for the Firebase is complete now.

Adding Note Model class

Note will be our domain model for the application.

Add a model class for Notes.

export class Note{
    id: string;
    title: string;
    content: string;
}

Let’s now create a service to handle the Firebase CRUD operations.

Creating Firebase Service to perform CRUD operations with Firestore

Create a new service class FirebaseService for handling Firebase CRUD operations.

ionic generate service services/firebase

or

ionic g s services/firebase

Create and initialize collectionName field to store Note’s collection name.

  private collectionName: string = "notes";

Pass AngularFirestore Dependency Injection in Service class constructor.

  constructor(private firestore: AngularFirestore) { }

Add addNote method for creating a note.

 addNote(note: Note) {
    return this.firestore.collection(this.collectionName).add({...note});
  }

Add getNote method to get a single note.

  getNote(id: string): Observable<Note>
  {
    return this.firestore.collection(this.collectionName).doc<Note>(id).snapshotChanges()
    .pipe(
      map(a => {
        const id = a.payload.id;
        const data = a.payload.data();
        return { id, ...data };
      })
    );
  }

Add getNotes method to get all the notes.

  getNotes(): Observable<Note[]> {
    return this.firestore.collection<Note>(this.collectionName).snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const id = a.payload.doc.id;
          const data = a.payload.doc.data();
          return { id, ...data };
        });
      })
    );
  }

Add updateNote method to update the note.

  updateNote(id: string, note: Note): Promise<void> {
   return this.firestore.collection(this.collectionName).doc<Note>(id).update(note);
  }

Add deleteNote method to delete a note.

  deleteNote(id: string): Promise<void> {
    return this.firestore.collection(this.collectionName).doc(id).delete();
  }

Completed FirebaseService service class should look as following.

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Note } from '../models/note';
@Injectable({
  providedIn: 'root'
})
export class FirebaseService {
  private collectionName: string = "notes";
  constructor(private firestore: AngularFirestore) { }
  addNote(note: Note) {
    return this.firestore.collection(this.collectionName).add({...note});
  }
  getNote(id: string): Observable<Note>
  {
    return this.firestore.collection(this.collectionName).doc<Note>(id).snapshotChanges()
    .pipe(
      map(a => {
        const id = a.payload.id;
        const data = a.payload.data();
        return { id, ...data };
      })
    );
  }
  getNotes(): Observable<Note[]> {
    return this.firestore.collection<Note>(this.collectionName).snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const id = a.payload.doc.id;
          const data = a.payload.doc.data();
          return { id, ...data };
        });
      })
    );
  }
  updateNote(id: string, note: Note): Promise<void> {
   return this.firestore.collection(this.collectionName).doc<Note>(id).update(note);
  }
  deleteNote(id: string): Promise<void> {
    return this.firestore.collection(this.collectionName).doc(id).delete();
  }
}

We are now ready to use this firebase service to perform CRUD operations from different ionic pages.

Creating a List Page to list all the Notes

Let’s create a new Ionic Page to list all the notes from Firebase.

ionic g page list-note

Add notes collection field to hold notes.

  notes: Observable<Note[]>;

Add FirebaseService as dependency injection to the List note component class

  constructor(private noteService: FirebaseService) {
  }

Initialize notes field.

  ngOnInit() {
    this.notes = this.noteService.getNotes();
  }

Following is the complete List Note Page component class.

import { Component, OnInit } from '@angular/core';
import { FirebaseService } from '../services/firebase.service';
import { Note } from '../models/note';
import { Observable } from 'rxjs';
@Component({
  selector: 'app-list-note',
  templateUrl: './list-note.page.html',
  styleUrls: ['./list-note.page.scss'],
})
export class ListNotePage implements OnInit {
  notes: Observable<Note[]>;
  constructor(private noteService: FirebaseService) {
  }
  ngOnInit() {
    this.notes = this.noteService.getNotes();
  }
}

Update List Note template with following.

<ion-list>
  <ion-item [routerLink]="['/display-note', item.id]" *ngFor="let item of (notes | async)">
    <ion-label>
      <h2>{{item.title}}</h2>
      <p>{{item.content}}</p>
    </ion-label>
  </ion-item>
</ion-list>

Add List Note Component to home page template. i.e. home.page.html

<ion-header [translucent]="true">
  <ion-toolbar color="primary">
    <ion-title>
      Notes
    </ion-title>
  </ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
  <app-list-note></app-list-note>
</ion-content>

Similarly we will create other pages for Get, Create, Update and Delete operations.

Creating Display Page to show single record from Firebase

Create a Display Page component.

ionic g page list-note

Display Note component class

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FirebaseService } from '../services/firebase.service';
import { Note } from '../models/note';
@Component({
  selector: 'app-display-note',
  templateUrl: './display-note.page.html',
  styleUrls: ['./display-note.page.scss'],
})
export class DisplayNotePage implements OnInit {
  note: Note;
  constructor(private router: Router, private activatedRoute: ActivatedRoute, private noteService: FirebaseService) {
    this.note = new Note();
  }
  ngOnInit() {
    this.noteService.getNote(this.activatedRoute.snapshot.params.id)
      .subscribe(data => {
        this.note = data;
      });
  }
}

Display note template

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>{{note.title}}</ion-title>
    <ion-buttons slot="end">
      <ion-button color="primary" [routerLink]="['/edit-note', note.id]">
        <ion-icon name="create-outline"></ion-icon>
      </ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
  <p>{{note.content}}</p>
</ion-content>

Creating Create Page to add a new record in Firebase

Create a Create Note Component.

ionic g page create-note

Update Create Note Component class with the following.

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { FirebaseService } from '../services/firebase.service';
import { Note } from '../models/note';
@Component({
  selector: 'app-create-note',
  templateUrl: './create-note.page.html',
  styleUrls: ['./create-note.page.scss'],
})
export class CreateNotePage implements OnInit {
  noteForm : FormGroup;
  constructor(private formBuilder: FormBuilder, private router: Router, private noteService: FirebaseService) {
    this.noteForm = this.formBuilder.group({
      title: new FormControl('', Validators.required),
      content: new FormControl('', Validators.required)
    });
  }
  ngOnInit() {
  }
  onSubmit() {
    const note: Note = Object.assign({}, this.noteForm.value);
    this.noteService.addNote(note)
      .then(_ => {
        this.router.navigate(['/home']);
      });
  }
  onReset(){
    this.noteForm.reset();
  }
}

Create Note Template

<ion-header>
  <ion-toolbar color="primary">
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>Add Note</ion-title>
    <ion-buttons slot="end" tooltip="Reset form" (click)="onReset()">
      <ion-icon name="refresh-outline"></ion-icon>
    </ion-buttons>
  </ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
  <form [formGroup]="noteForm" (ngSubmit)="onSubmit()" novalidate>
    <ion-item>
      <ion-input placeholder="Enter notes title" autofocus=true formControlName="title"></ion-input>
    </ion-item>
  
    <ion-item>
      <ion-textarea placeholder="Enter your notes here..." rows="10" formControlName="content"></ion-textarea>
    </ion-item>
  
    <ion-button color="primary" class="ion-float-left" type="submit" [disabled] = !noteForm.valid>Save</ion-button>
  </form>
</ion-content>

Update home.page.html to add a Fab Add button.

<ion-header [translucent]="true">
  <ion-toolbar color="primary">
    <ion-title>
      Notes
    </ion-title>
  </ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
  <app-list-note></app-list-note>
  <!-- fab placed to the top end -->
  <ion-fab vertical="bottom" horizontal="end" slot="fixed">
    <ion-fab-button [routerLink]="['/create-note']">
      <ion-icon name="add"></ion-icon>
    </ion-fab-button>
  </ion-fab>
</ion-content>

Update create-note.module.ts to add ReactiveFormsModule.

We are using Reactive Forms for forms in Angular.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { CreateNotePageRoutingModule } from './create-note-routing.module';
import { CreateNotePage } from './create-note.page';
@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    IonicModule,
    CreateNotePageRoutingModule
  ],
  declarations: [CreateNotePage]
})
export class CreateNotePageModule {}

Creating Update Page to update a record in the Firebase.

Create Update Note page component.

ionic g page update-note

Update Note Component Class

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { FirebaseService } from '../services/firebase.service';
import { Note } from '../models/note';
@Component({
  selector: 'app-edit-note',
  templateUrl: './edit-note.page.html',
  styleUrls: ['./edit-note.page.scss'],
})
export class EditNotePage implements OnInit {
  id: string='';
  noteForm : FormGroup;
  constructor(private formBuilder: FormBuilder, private router: Router, private activatedRoute: ActivatedRoute, private noteService: FirebaseService) {
    this.noteForm = this.formBuilder.group({
      title: new FormControl('', Validators.required),
      content: new FormControl('', Validators.required)
    });
  }
  ngOnInit() {
    this.id= this.activatedRoute.snapshot.paramMap.get("id");
    this.noteService.getNote(this.activatedRoute.snapshot.paramMap.get("id"))
      .subscribe(data => {
        this.noteForm = this.formBuilder.group({
          title: new FormControl(data.title, Validators.required),
          content: new FormControl(data.content, Validators.required)
        });
      });
  }
  onSubmit() {
    const note: Note = Object.assign({}, this.noteForm.value);
    this.noteService.updateNote(this.id, note)
    .then(()=>{
      this.router.navigate(['/home']);
    });
  }
}

Update Note Template

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>Edit Note</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
  <form [formGroup]="noteForm" (ngSubmit)="onSubmit()" novalidate>
    <ion-item>
      <ion-input placeholder="Enter notes title" autofocus=true formControlName="title"></ion-input>
    </ion-item>
  
    <ion-item>
      <ion-textarea placeholder="Enter your notes here..." rows="10" formControlName="content"></ion-textarea>
    </ion-item>
  
    <ion-button color="primary" class="ion-float-left" type="submit" [disabled] = !noteForm.valid>Save</ion-button>
  </form>
</ion-content>

Update create-note.module.ts to add ReactiveFormsModule

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { EditNotePageRoutingModule } from './edit-note-routing.module';
import { EditNotePage } from './edit-note.page';
@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    IonicModule,
    EditNotePageRoutingModule
  ],
  declarations: [EditNotePage]
})
export class EditNotePageModule {}

Deleting a Record Using Delete Operation

To Delete note we will add a Delete button on Display Note Page.

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>{{note.title}}</ion-title>
    <ion-buttons slot="end">
      <ion-button color="primary" [routerLink]="['/edit-note', note.id]">
        <ion-icon name="create-outline"></ion-icon>
      </ion-button>
      <ion-button color="danger" (click)="onDelete()">
        <ion-icon name="trash-outline"></ion-icon>
      </ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<p>{{note.content}}</p>
</ion-content>

Update display-note.page.ts Component Class for delete method.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FirebaseService } from '../services/firebase.service';
import { Note } from '../models/note';
@Component({
  selector: 'app-display-note',
  templateUrl: './display-note.page.html',
  styleUrls: ['./display-note.page.scss'],
})
export class DisplayNotePage implements OnInit {
  note: Note;
  constructor(private router: Router, private activatedRoute: ActivatedRoute, private noteService: FirebaseService) {
    this.note = new Note();
  }
  ngOnInit() {
    this.noteService.getNote(this.activatedRoute.snapshot.params.id)
      .subscribe(data => {
        this.note = data;
      });
  }
  onDelete(){
    this.noteService.deleteNote(this.note.id)
    .then(()=>{
      this.router.navigate(['/home']);
    });
  }
}

Conclusion

In this post we learned how to perform the CRUD operations in Firestore database inside Firebase using Ionic 5 application which was created using Angular 12.

We started with setting up the Firebase project to get firestore database settings. Then we created the ionic 5 application using ionic CLI and then added support for all of the Read, Create, Update and Delete operations.

Please leave your feedback or comment if you are facing any issue.