【问题标题】:How to use angular with cloud firebase function in localhost如何在 localhost 中使用 Angular 和 Cloud Firebase 功能
【发布时间】:2019-07-10 10:15:38
【问题描述】:

我在 localhost 中使用 Angular 和 firebase 函数时遇到问题。我想要的是如果用户删除他的帐户,那么firebase函数应该做出反应(比如删除firestore db中已删除用户的所有内容)。例如:

角度:

public deleteAccount(): Promise<void> {
    return this.angularFireAuth.auth.currentUser.delete().then(() => {

      const test = this.angularFireFunctions.httpsCallable('httpFunction');
      test(null).subscribe((response) => {
        debugger;
      }, (error) => {
        debugger;
      });
    }).catch((error) => {
      this.uiService.showSnackBar(error.message, 'error-snackbar');
    });
  }

功能:

export const httpFunction = functions.https.onRequest((req, res) => {
  cors(req, res, () => { // need cors, because of error in console
    return functions.auth.user().onDelete((user: admin.auth.UserRecord) => {
      return res.status(200).send(user.uid);
    });
  });
});

但是即使我使用这样的代码只是为了测试,删除用户后,有一个对httpFunction的请求,它是可以的,但状态一直是'pending'。

第二件事,在firestore控制台(在功能部分的日志中)有如下信息:

“未配置计费帐户。无法访问外部网络并且配额受到严重限制。配置计费帐户以消除这些限制”

"函数执行耗时 60002 毫秒,完成状态为:'timeout'"

这个帐户是免费的,我认为这个消息只是一个警报,但无论如何,你知道如何在 Cloud firestore 功能中正确使用 onDelete 吗?

【问题讨论】:

    标签: angular google-cloud-firestore google-cloud-functions angularfire2


    【解决方案1】:

    我强烈建议您通读有关后台函数/触发器的 Firebase 文档。 onUserDeleted 是一个触发器。

    https://cloud.google.com/functions/docs/concepts/events-triggers

    **** 这里是 TL;DR: ****

    您无需通过 https 调用手动触发后台函数,因为它们会自动触发以响应事件。后台函数通过从入口点 (index.(ts|js)) 导出它们来工作。

    所以,只需从 index.(ts|js) 导出您的函数并部署它。

    export const onUserDeleted = functions.auth.user().onDelete((user: admin.auth.UserRecord) => {
          // Delete user in Firestore
          // Don't forget to return a Promise!
        });
      });
    });
    

    onUserDeleted 将被触发以响应删除用户。在你的情况下,在 Angular 中调用 this.angularFireAuth.auth.currentUser.delete()

    您可以通过在 onUserDeleted 回调中使用 console.log 并在 firebase 控制台中的函数 -> 日志下查看它来测试它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 2022-06-25
      • 2020-04-19
      • 2018-07-01
      • 2020-07-18
      • 2017-10-27
      • 1970-01-01
      相关资源
      最近更新 更多