问题描述
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
或更高版本 - 因为这是 PR 已合并.
Make sure you update your angularfire2
version to RC9
or higher - as this is when the PR was merged.
npm install angularfire2@latest
将 AngularFireFunctions
添加到 app.module
中的 providers
.
Add AngularFireFunctions
to your providers
in your app.module
.
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 执行可调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!