简体   繁体   English

如何在 firebase 中为 auth.user().onCreate() 触发器创建日志记录 function?

[英]How can I create a logging function in firebase for auth.user().onCreate() trigger?

I am trying to log each time an account is created and deleted.每次创建和删除帐户时,我都试图记录。

I created a trigger functions.auth.user().onCreate() and as I understand it returns an user object as in the docs: here , and here .我创建了一个触发器functions.auth.user().onCreate() ,据我所知,它返回一个user object,如文档中所示: here和 here

The functions deploy without trouble but when the trigger is called it throws an error:函数部署没有问题,但是当调用触发器时它会抛出错误:

Error: Process exited with code 16
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:92:22)
at process.emit (events.js:314:20)
at process.EventEmitter.emit (domain.js:483:12)
at process.exit (internal/process/per_thread.js:168:15)
at sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:44:9)
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:88:44)
at process.emit (events.js:314:20)
at process.EventEmitter.emit (domain.js:483:12)
at processPromiseRejections (internal/process/promises.js:209:33)
at processTicksAndRejections (internal/process/task_queues.js:98:32) 

Error which I cannot understand.我无法理解的错误。

Here is my code这是我的代码

// functions/index.js    
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const { google } = require('googleapis')
const { firestore } = require("firebase-admin");

exports.logging = require('./logging');

admin.initializeApp()
// And other working functions

The actual functions实际功能

    // functions/logging.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { firestore } = require('firebase-admin');

const authUserTrigger = functions.auth.user()

exports.userSignup = authUserTrigger.onCreate((user) => {
  storeUser(user)
})

exports.userDelete = authUserTrigger.onDelete((user) => {
  storeUser(user)
})

    async function storeUser(user) {
      // functions.logger.log(user.email) -> this works

      // Destructured original object
      let updatedUser = (({ displayName, email }) => ({ displayName, email }))(user);
      functions.logger.log('updatedUser', updatedUser )
      await admin
        .firestore()
        .collection('logs')
        .doc('users')
        .collection('signup')
        .set({
          user: {updatedUser}, // I think this is the culprint
          // user,  This doesn't work either
          createTimestamp: firestore.FieldValue.serverTimestamp()
        }, { merge: true })
    
    };

Thank you in advance先感谢您

EDIT ========== @Tanay was right.编辑 ========== @Tanay 是对的。 Needed to change set to add .需要更改setadd .

As @Tanay stated, you cannot use set() in a collection in Firebase, it must be a document.正如@Tanay 所述,您不能在 Firebase 的集合中使用set() ,它必须是文档。 If you want to add a document to the collection with an auto ID then you can use add() on the collection with the data.如果您想使用自动 ID 将文档添加到集合中,则可以对包含数据的集合使用add()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建与 auth.user 同步的用户文档 onCreate 和 onUpdate Firebase v9 - Create a user document onCreate and onUpdate that is in sync with the auth.user Firebase v9 Firebase functions.auth.user().onCreate 无触发 - Firebase functions.auth.user().onCreate no triggering 我可以在 Cloud Functions Http 触发器中创建 Firebase 身份验证用户吗? - Can I create a user on Firebase Authentication in Cloud Functions Http Trigger? Firebase Cloud Function,onCreate事件触发后如何访问父数据? - Firebase Cloud Function, how can I access parent data after onCreate event triggered? 如何创建 firebase 用户帐户 - How can I create firebase user account 我们可以在 Firebase 身份验证触发器(即 onCreate)上访问请求 header 吗? - Can we access request header on Firebase Auth triggers i.e onCreate? Firebase Auth,从反应客户端登录用户后,如何验证用户在我的其他 api 中是否合法? - Firebase Auth, after logging a user from react client, how do I verify user is legitimate within my other api? Firebase:如何在 onCreate 中获取附加用户信息 Firebase 云 function - Firebase: how to get getAdditionalUserInfo in onCreate Firebase Cloud function 无法创建新的 Microsoft 用户 Firebase Auth - Unable to create new Microsoft user Firebase Auth 我可以通过 Firebase 授权用户并使用 Supabase(私有)存储桶而不依赖 Supabase Auth 用户限制吗 - Can I Auth a user through Firebase and use Supabase (private) buckets without relying on Supabase Auth users limitation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM