Firebase Cloud Function:Auth回调存在问题。

huangapple go评论56阅读模式
英文:

Firebase Cloud Function: An issue with Auth callbacks

问题

我有两个Auth回调函数,一个在用户创建时触发,另一个在用户删除时触发。每个回调都成功触发,但产生相同的错误:“TypeError: handler is not a function at cloudFunction”。看起来我在回调函数的语法上有误,但我找不到适用于V1函数的正确示例。请帮忙,谢谢。

另外:我如何将这些函数部署到europe-west1而不是us-central1,V1函数是否支持这个功能?

const functions = require("firebase-functions"); // v1
const { initializeApp } = require("firebase-admin/app");
initializeApp();

const db = getFirestore();

exports.authUserCreated = functions.auth.user().onCreate({ region: "europe-west1" }, (user) => {
  const userEmail = user.email;
  const userId = user.uid;
  return db.collection("users").doc(userId).update({ createdAt: FieldValue.serverTimestamp(), email: userEmail });
});

exports.authUserDeleted = functions.auth.user().onDelete({ region: "europe-west1" }, (user) => {
  const userId = user.uid;
  return db.collection("users").doc(userId).delete();
});
英文:

I have two Auth callbacks, when a user was created and deleted. Each callback fired successfully, but produces the same error: TypeError: handler is not a function at cloudFunction. It seems I was wrong with callbacks' syntax, but I can't find right example for V1 functions. Please help, thanks.

Also: how can I deploy these funcs to europe-west1 insted of us-central1, is it possible for V1 functions?

const functions = require("firebase-functions"); // v1
const {initializeApp} = require("firebase-admin/app");
initializeApp();

const db = getFirestore();

exports.authUserCreated = functions.auth.user().onCreate({region: "europe-west1"}, (user) => {
  const userEmail = user.email;
  const userId = user.uid;
  return db.collection("users").doc(userId).update({createdAt: FieldValue.serverTimestamp(), email: userEmail});
});

exports.authUserDeleted = functions.auth.user().onDelete({region: "europe-west1"}, (user) => {
  const userId = user.uid;
  return db.collection("users").doc(userId).delete();
});

答案1

得分: 1

你不能将对象作为onCreate()的第一个参数传递。这是v2语法,但您正在编写v1函数。请按照文档中的示例进行操作:

exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
  // ...
});

只有一个参数 - 处理程序函数。

如果您想要在v1类型的函数上设置区域,也有相关的文档

exports.myStorageFunction = functions
    .region('europe-west1')
    .storage
    .object()
    .onFinalize((object) => {
      // ...
    });

因此,您需要执行类似以下的操作:

exports.authUserCreated = functions
  .region("europe-west1")
  .auth
  .user()
  .onCreate(user => { ... })

您还可以查看firebase-functions模块的API文档以获取更多信息。

英文:

You can't pass an object as the first argument to onCreate(). That's v2 syntax, but you are writing a v1 function. Follow the example in the documentation:

exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
  // ...
});

There is only one argument - the handler function.

If you want to set the region on a v1 type function, there is documentation for that as well:

exports.myStorageFunction = functions
    .region('europe-west1')
    .storage
    .object()
    .onFinalize((object) => {
      // ...
    });

So you will need to do something like this:

exports.authUserCreated = functions
  .region("europe-west1")
  .auth
  .user()
  .onCreate(user => { ... })

You might also want to review the API documentation for the firebase-functions module.

huangapple
  • 本文由 发表于 2023年7月18日 01:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定