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

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

Firebase Cloud Functions V2: A user creation trigger

问题

  1. 我正在使用 Node.js(不是 TypeScript)的云函数 V2,并且我需要用户创建触发器。在[文档][1]中,我找到了一些内容,但它不起作用。通过链接,我还看到了以下提示:
  2. > Firebase Cloud Functions(第二代)**不支持**此指南中描述的事件和触发器。
  3. 对于 V2 云函数来说,Auth 触发器不再起作用吗?如果是这样,有没有替代的功能?以下是我的代码和部署错误:
  4. ```javascript
  5. const {functions} = require("firebase-functions");
  6. initializeApp();
  7. const db = getFirestore();
  8. exports.authUserCreated = functions.auth.user().onCreate({region: "europe-west1"}, (user) => {
  9. const userEmail = user.email;
  10. const userId = user.uid;
  11. return db.collection("users").doc(userId).update({createdAt: FieldValue.serverTimestamp(), email: userEmail});
  12. });

部署错误:TypeError: 无法读取未定义的属性(读取 'auth')

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. &gt; Cloud Functions for Firebase (2nd gen) **does not provide** support for
  5. &gt; the events and triggers described in this guide.
  6. 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:
  7. const {functions} = require(&quot;firebase-functions&quot;);
  8. initializeApp();
  9. const db = getFirestore();
  10. exports.authUserCreated = functions.auth.user().onCreate({region: &quot;europe-west1&quot;}, (user) =&gt; {
  11. const userEmail = user.email;
  12. const userId = user.uid;
  13. return db.collection(&quot;users&quot;).doc(userId).update({createdAt: FieldValue.serverTimestamp(), email: userEmail});
  14. });
  15. Deploy error: `TypeError: Cannot read properties of undefined (reading &#39;auth&#39;)`
  16. [1]: https://firebase.google.com/docs/functions/auth-events
  17. </details>
  18. # 答案1
  19. **得分**: 1
  20. Your `require` statement is wrong. It should be:
  21. ```javascript
  22. const functions = require('firebase-functions');
英文:

Your require statement is wrong. It should be:

  1. 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:

确定