英文:
Firebase Cloud Functions V2: A user creation trigger
问题
我正在使用 Node.js(不是 TypeScript)的云函数 V2,并且我需要用户创建触发器。在[文档][1]中,我找到了一些内容,但它不起作用。通过链接,我还看到了以下提示:
> Firebase 的 Cloud Functions(第二代)**不支持**此指南中描述的事件和触发器。
对于 V2 云函数来说,Auth 触发器不再起作用吗?如果是这样,有没有替代的功能?以下是我的代码和部署错误:
```javascript
const {functions} = require("firebase-functions");
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});
});
部署错误:TypeError: 无法读取未定义的属性(读取 'auth')
<details>
<summary>英文:</summary>
I'm using cloud functions V2 with Node.js (not Typescript) and I need user creation trigger. In [1] I've found something, but it doesn't work. By the link I also can see the following hint there:
> Cloud Functions for Firebase (2nd gen) **does not provide** support for
> the events and triggers described in this guide.
Auth triggers doesn't work anymore for V2 cloud functions? If so, what is an alternative for this functionality? Below my code and deploy error:
const {functions} = require("firebase-functions");
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});
});
Deploy error: `TypeError: Cannot read properties of undefined (reading 'auth')`
[1]: https://firebase.google.com/docs/functions/auth-events
</details>
# 答案1
**得分**: 1
Your `require` statement is wrong. It should be:
```javascript
const functions = require('firebase-functions');
英文:
Your require
statement is wrong. It should be:
const functions = require('firebase-functions');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论