【问题标题】:firebase firestore http functionfirebase firestore http函数
【发布时间】:2019-06-24 01:02:00
【问题描述】:

我正在从我的 Angular 应用程序调用 firebase firestore http 函数,但它总是返回错误。

service.ts

 constructor(private http: HttpClient, private afs: AngularFirestore,
 private fns: AngularFireFunctions, private afstorage: AngularFireStorage) { }
call(url: string) {

this.fns.httpsCallable(url)({ text: 'some-data' })
  .pipe(first())
  .subscribe(resp => {
    console.log({ resp });
  }, err => {
    console.log({ error: err });
  });

}

index.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp()
const cors = require('cors')({ origin: true });

export const hello = functions.region('asia- 
northeast1').https.onRequest((data, context) => {
    cors(data, context, () => { '' })
    console.log('execution started');
    console.log('data', data);
    return;

});

【问题讨论】:

    标签: typescript google-cloud-firestore angular6 google-cloud-functions


    【解决方案1】:

    您似乎正在尝试使用 onRequest 函数触发器,这需要带有响应和请求参数的回调。您需要使用response.send() 将某些内容发送回客户端。

    functions.https.onRequest((request, response) => {
      response.send('Success!');
    });
    

    或者,您正在使用的结构与 https.onCall() 一起使用

    onRequest 的文档:https://firebase.google.com/docs/functions/http-events onCall 的文档:https://firebase.google.com/docs/functions/callable

    【讨论】:

    • 如何编写带有 onCall 请求的函数?你能解释一下吗
    • Richard 提供的第二个链接显示了有关创建 Callable 函数的文档。
    • 我使用链接中给出的示例编写了一个函数,它总是给出错误错误:{status: "INVALID_ARGUMENT", message: "Bad Request"},我的代码有什么问题?
    • 我不熟悉 Angular 实现,但是使用 firebase 客户端 SDK,您可以执行如下可调用函数:firebase.functions().httpsCallable('functionName')(requestData).then(...)
    猜你喜欢
    • 2021-11-17
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多