首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在firebase中为auth.user().onCreate()触发器创建日志记录函数?

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

Stack Overflow用户
提问于 2022-04-16 01:38:43
回答 1查看 110关注 0票数 0

每次创建和删除帐户时,我都会尝试记录。

我创建了一个触发器functions.auth.user().onCreate(),据我所知,它返回一个user对象,如文档中的:这里还有这里

函数的部署没有问题,但是当调用触发器时,它会抛出一个错误:

代码语言:javascript
运行
复制
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) 

我无法理解的错误。

这是我的密码

代码语言:javascript
运行
复制
// 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

实际职能

代码语言:javascript
运行
复制
    // 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 })
    
    };

提前谢谢你

编辑========== @Tanay是对的。需要将set更改为add

EN

回答 1

Stack Overflow用户

发布于 2022-04-19 06:05:36

正如@Tanay所述,您不能在Firebase的集合中使用set(),它必须是文档。如果要使用自动ID向集合添加文档,则可以在集合上使用add()和数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71890406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档