Skip to content

Commit e9200a7

Browse files
authored
Updating callable functions documentation
In `app.module.ts` I put in commented out lines anticipating AngularFire 7. When AngularFire 7 is available for callable functions we can easily update the documentation with a few clicks. I also put in two lines showing how to run your functions in the Firebase emulator. I clearly indicated how to comment out these lines to run your functions in the cloud. I made an HTML view to show how to handle user input. Also I added a second set of curly brackets to `{{ data$ | async }}`. This is necessary to make the code run without throwing errors. In `app.component.ts` I put in lines anticipating AngularFire 7. I moved the template to an HTML view. I added a variable `data$` to handle the data returned from the cloud function. I added `this` to `fns.httpsCallable('my-fn-name');`. These changes are necessary to make the code run without throwing errors. I changed `fns` to `functions` for readability. I made two functions, one that executes on page load and the user executes on user input. I put in the `index.js` file showing the cloud functions. The old documentation was confusing as to what data went from Angular to the cloud function and what data was returned from the cloud function. Showing the `index.js` functions clarifies this. I've written a longer tutorial at https://github.com/tdkehoe/Firebase-Cloud-Functions-with-Angular/blob/main/README.md. When can we expect to use AngularFire 7 with callable functions?
1 parent 89fd6e9 commit e9200a7

File tree

1 file changed

+97
-10
lines changed

1 file changed

+97
-10
lines changed

docs/functions/functions.md

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,58 @@
77
Cloud Functions for AngularFire is contained in the `@angular/fire/functions` module namespace. Import the `AngularFireFunctionsModule` in your `NgModule`. This sets up the `AngularFireFunction` service for dependency injection.
88

99
```ts
10-
import { BrowserModule } from '@angular/platform-browser';
1110
import { NgModule } from '@angular/core';
11+
import { BrowserModule } from '@angular/platform-browser';
1212
import { AppComponent } from './app.component';
13+
import { environment } from '../environments/environment';
14+
15+
// AngularFire 7
16+
// import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
17+
// import { provideFirestore, getFirestore } from '@angular/fire/firestore';
18+
// import { provideFunctions, getFunctions, connectFunctionsEmulator } from '@angular/fire/functions'; // https://firebase.google.com/docs/emulator-suite/connect_functions#instrument_your_app_to_talk_to_the_emulators
19+
20+
// AngularFire 6
1321
import { AngularFireModule } from '@angular/fire/compat';
1422
import { AngularFireFunctionsModule } from '@angular/fire/compat/functions';
15-
import { environment } from '../environments/environment';
23+
import { USE_EMULATOR } from '@angular/fire/compat/functions'; // comment out to run in the cloud
1624

1725
@NgModule({
26+
declarations: [
27+
AppComponent
28+
],
1829
imports: [
1930
BrowserModule,
31+
32+
// AngularFire 7
33+
// provideFirebaseApp(() => initializeApp(environment.firebase)),
34+
// provideFirestore(() => getFirestore()),
35+
// provideFunctions(() => getFunctions()),
36+
37+
// AngularFire 6
2038
AngularFireModule.initializeApp(environment.firebase),
2139
AngularFireFunctionsModule
2240
],
23-
declarations: [ AppComponent ],
24-
bootstrap: [ AppComponent ]
41+
providers: [
42+
{ provide: USE_EMULATOR, useValue: ['localhost', 5001] } // comment out to run in the cloud
43+
],
44+
bootstrap: [AppComponent]
2545
})
26-
export class AppModule {}
46+
export class AppModule { }
47+
```
48+
49+
Comment out two lines to run your functions in the cloud instead of in the Firebase emulator.
50+
51+
### Make an HTML view
52+
```html
53+
<div>
54+
<button mat-raised-button color="basic" (click)='callMe()'>Call me!</button>
55+
</div>
56+
57+
{{ data$ | async }}
2758
```
2859

60+
This view has a button for user input and displays the data returned from the cloud function.
61+
2962
### Injecting the AngularFireFunctions service
3063

3164
Once the `AngularFireFunctionsModule` is registered you can inject the `AngularFireFunctions` service.
@@ -50,25 +83,79 @@ AngularFireFunctions is super easy. You create a function on the server side and
5083
| method | |
5184
| ---------|--------------------|
5285
| `httpCallable(name: string): (data: T) ` | Creates a callable function based on a function name. Returns a function that can create the observable of the http call. |
53-
```ts
5486

87+
```ts
5588
import { Component } from '@angular/core';
89+
90+
// AngularFire 7
91+
// import { getApp } from '@angular/fire/app';
92+
// import { provideFunctions, getFunctions, connectFunctionsEmulator, httpsCallable } from '@angular/fire/functions'; // https://firebase.google.com/docs/emulator-suite/connect_functions#instrument_your_app_to_talk_to_the_emulators
93+
// import { Firestore, doc, getDoc, getDocs, collection, updateDoc } from '@angular/fire/firestore';
94+
95+
// AngularFire 6
5696
import { AngularFireFunctions } from '@angular/fire/compat/functions';
5797

5898
@Component({
5999
selector: 'app-root',
60-
template: `{ data$ | async }`
100+
templateUrl: './app.component.html',
61101
})
62102
export class AppComponent {
63-
constructor(private fns: AngularFireFunctions) {
64-
const callable = fns.httpsCallable('my-fn-name');
65-
this.data$ = callable({ name: 'some-data' });
103+
data$: any;
104+
105+
constructor(private functions: AngularFireFunctions) {
106+
const callable = this.functions.httpsCallable('executeOnPageLoad');
107+
this.data$ = callable({ name: 'Charles Babbage' });
66108
}
109+
110+
callMe() {
111+
console.log("Calling...");
112+
const callable = this.functions.httpsCallable('callMe');
113+
this.data$ = callable({ name: 'Ada Lovelace' });
114+
};
67115
}
68116
```
69117

118+
`data$` handles the data returned from the cloud function and displays the data in the view.
119+
120+
The component handles two functions, one that executes on page load and another that executes on user input.
121+
70122
Notice that calling `httpsCallable()` does not initiate the request. It creates a function, which when called creates an Observable, subscribe or convert it to a Promise to initiate the request.
71123

124+
### Make your callable cloud functions
125+
126+
```js
127+
// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
128+
const functions = require('firebase-functions');
129+
130+
// The Firebase Admin SDK to access Firestore.
131+
const admin = require('firebase-admin');
132+
admin.initializeApp();
133+
134+
// executes on page load
135+
exports.executeOnPageLoad = functions.https.onCall((data, context) => {
136+
console.log("The page is loaded!")
137+
console.log(data);
138+
console.log(data.name);
139+
// console.log(context);
140+
return 22
141+
});
142+
143+
// executes on user input
144+
exports.callMe = functions.https.onCall((data, context) => {
145+
console.log("Thanks for calling!")
146+
console.log(data);
147+
console.log(data.name);
148+
// console.log(context);
149+
return 57
150+
});
151+
```
152+
153+
The first function executes when the page loads. The second function executes from user input.
154+
155+
Both functions use `https.onCall((data, context) => {})`, which makes a function callable from Angular.
156+
157+
`data` is the data sent from Angular. `context` is metadata about the function executing. The returned data (`22`, `57`) goes back to Angular and is displayed in the view. This data is an Observable.
158+
72159
## Configuration via Dependency Injection
73160

74161
### Functions Region

0 commit comments

Comments
 (0)