问题
Callable functions in firebase can be executed as below:
firebase.functions().httpsCallable('addMessage');
I am wondering what is the equivalent of this in AngularFire2. I have scanned the documents and don't see any mention of this.
If there is no equivalent then how do i obtain a handle to underlying firebase object in AngularFire2 ?
回答1:
Make sure you update your angularfire2
version to RC9
or higher - as this is when the PR was merged.
npm install angularfire2@latest
Add AngularFireFunctions
to your providers
in your app.module
.
import { AngularFireFunctions } from 'angularfire2/functions';
@NgModule({
providers: [
AngularFireFunctions
],
});
Within your Component you can run httpsCallable
function from AngularFireFunctions
.
constructor(
private afFun: AngularFireFunctions,
) { }
ngOnInit() {
// Angular Fire - Converts Promise to Observable
this.afFun.httpsCallable('myFunction')({ text: 'Some Request Data' })
.pipe(first())
.subscribe(resp => {
console.log({ resp });
}, err => {
console.error({ err });
});
// Convert back to Promise
const respRef = await this.afFun.httpsCallable('myFunction')({ text: 'Some Request Data' })
.toPromise();
console.log({ respRef });
}
A comment in the AngularFire2 repo has asked why the response has been converted to an Observable
. The Firebase documentation has the function returning a Promise
- so be aware of the inconsistency - it could change in the future.
来源:https://stackoverflow.com/questions/51456237/how-to-execute-callable-functions-using-angularfire2