Skip to content

Commit 62e1425

Browse files
committed
Merge branch 'patch-2' of https://github.com/tdkehoe/angularfire into tdkehoe-patch-2
2 parents 59b95af + e9200a7 commit 62e1425

File tree

1 file changed

+97
-10
lines changed

1 file changed

+97
-10
lines changed

docs/compat/functions/functions.md

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,58 @@
99
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.
1010

1111
```ts
12-
import { BrowserModule } from '@angular/platform-browser';
1312
import { NgModule } from '@angular/core';
13+
import { BrowserModule } from '@angular/platform-browser';
1414
import { AppComponent } from './app.component';
15+
import { environment } from '../environments/environment';
16+
17+
// AngularFire 7
18+
// import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
19+
// import { provideFirestore, getFirestore } from '@angular/fire/firestore';
20+
// 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
21+
22+
// AngularFire 6
1523
import { AngularFireModule } from '@angular/fire/compat';
1624
import { AngularFireFunctionsModule } from '@angular/fire/compat/functions';
17-
import { environment } from '../environments/environment';
25+
import { USE_EMULATOR } from '@angular/fire/compat/functions'; // comment out to run in the cloud
1826

1927
@NgModule({
28+
declarations: [
29+
AppComponent
30+
],
2031
imports: [
2132
BrowserModule,
33+
34+
// AngularFire 7
35+
// provideFirebaseApp(() => initializeApp(environment.firebase)),
36+
// provideFirestore(() => getFirestore()),
37+
// provideFunctions(() => getFunctions()),
38+
39+
// AngularFire 6
2240
AngularFireModule.initializeApp(environment.firebase),
2341
AngularFireFunctionsModule
2442
],
25-
declarations: [ AppComponent ],
26-
bootstrap: [ AppComponent ]
43+
providers: [
44+
{ provide: USE_EMULATOR, useValue: ['localhost', 5001] } // comment out to run in the cloud
45+
],
46+
bootstrap: [AppComponent]
2747
})
28-
export class AppModule {}
48+
export class AppModule { }
49+
```
50+
51+
Comment out two lines to run your functions in the cloud instead of in the Firebase emulator.
52+
53+
### Make an HTML view
54+
```html
55+
<div>
56+
<button mat-raised-button color="basic" (click)='callMe()'>Call me!</button>
57+
</div>
58+
59+
{{ data$ | async }}
2960
```
3061

62+
This view has a button for user input and displays the data returned from the cloud function.
63+
3164
### Injecting the AngularFireFunctions service
3265

3366
Once the `AngularFireFunctionsModule` is registered you can inject the `AngularFireFunctions` service.
@@ -52,25 +85,79 @@ AngularFireFunctions is super easy. You create a function on the server side and
5285
| method | |
5386
| ---------|--------------------|
5487
| `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. |
55-
```ts
5688

89+
```ts
5790
import { Component } from '@angular/core';
91+
92+
// AngularFire 7
93+
// import { getApp } from '@angular/fire/app';
94+
// 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
95+
// import { Firestore, doc, getDoc, getDocs, collection, updateDoc } from '@angular/fire/firestore';
96+
97+
// AngularFire 6
5898
import { AngularFireFunctions } from '@angular/fire/compat/functions';
5999

60100
@Component({
61101
selector: 'app-root',
62-
template: `{ data$ | async }`
102+
templateUrl: './app.component.html',
63103
})
64104
export class AppComponent {
65-
constructor(private fns: AngularFireFunctions) {
66-
const callable = fns.httpsCallable('my-fn-name');
67-
this.data$ = callable({ name: 'some-data' });
105+
data$: any;
106+
107+
constructor(private functions: AngularFireFunctions) {
108+
const callable = this.functions.httpsCallable('executeOnPageLoad');
109+
this.data$ = callable({ name: 'Charles Babbage' });
68110
}
111+
112+
callMe() {
113+
console.log("Calling...");
114+
const callable = this.functions.httpsCallable('callMe');
115+
this.data$ = callable({ name: 'Ada Lovelace' });
116+
};
69117
}
70118
```
71119

120+
`data$` handles the data returned from the cloud function and displays the data in the view.
121+
122+
The component handles two functions, one that executes on page load and another that executes on user input.
123+
72124
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.
73125

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

76163
### Functions Region

0 commit comments

Comments
 (0)