简体   繁体   中英

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 .

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. Needed to change set to add .

As @Tanay stated, you cannot use set() in a collection in Firebase, it must be a document. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM