[英]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
.需要更改set
以add
.
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.