如何使用AngularFire2执行可调用函数 [英] How to execute callable functions using AngularFire2
问题描述
firebase中的可调用函数可以按以下方式执行:
Callable functions in firebase can be executed as below:
firebase.functions().httpsCallable('addMessage');
我想知道AngularFire2中的等效功能是什么.我已经扫描了文档,对此一无所获.
I am wondering what is the equivalent of this in AngularFire2. I have scanned the documents and don't see any mention of this.
如果没有等效项,那么如何在AngularFire2中获取基础Firebase对象的句柄?
If there is no equivalent then how do i obtain a handle to underlying firebase object in AngularFire2 ?
推荐答案
请确保将angularfire2
版本更新为RC9
或更高版本-因为这是
Make sure you update your angularfire2
version to RC9
or higher - as this is when the PR was merged.
npm install angularfire2@latest
在app.module
中的providers
中添加AngularFireFunctions
.
import { AngularFireFunctions } from 'angularfire2/functions';
@NgModule({
providers: [
AngularFireFunctions
],
});
在组件中,您可以从AngularFireFunctions
运行httpsCallable
函数.
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 });
}
AngularFire2存储库中的评论已问到为什么响应已转换为Observable
. Firebase文档具有返回Promise
的功能-请注意不一致-将来可能会改变.
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.
这篇关于如何使用AngularFire2执行可调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!