Firebase Cloud Functions V2:用户创建触发器

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

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&#39;m using cloud functions V2 with Node.js (not Typescript) and I need user creation trigger. In 
[1] I&#39;ve found something, but it doesn&#39;t work. By the link I also can see the following hint there: &gt; Cloud Functions for Firebase (2nd gen) **does not provide** support for &gt; the events and triggers described in this guide. Auth triggers doesn&#39;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(&quot;firebase-functions&quot;); initializeApp(); const db = getFirestore(); exports.authUserCreated = functions.auth.user().onCreate({region: &quot;europe-west1&quot;}, (user) =&gt; { const userEmail = user.email; const userId = user.uid; return db.collection(&quot;users&quot;).doc(userId).update({createdAt: FieldValue.serverTimestamp(), email: userEmail}); }); Deploy error: `TypeError: Cannot read properties of undefined (reading &#39;auth&#39;)` [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(&#39;firebase-functions&#39;);

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

发表评论

匿名网友

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

确定