通过 Firestore 函数 onCreate 向用户配置文件添加其他数据 [英] Add additional data to user profile via firestore functions onCreate

本文介绍了通过 Firestore 函数 onCreate 向用户配置文件添加其他数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户通过 firestore 功能注册时,我正在尝试在我的 firestore 数据库中自动创建用户数据.但是我有 2 个注册选项.商业账户和个人账户.如果用户注册为企业,那么我想将usertype = 1"添加到我的文档中,当个人时,usertype = 2"我设法通过函数自动创建一个注册文档

I'm trying to automatically create a user data in my firestore db when user sign up through firestore functions. However I got 2 option for sign up. Bussiness account and personal account. If user signup as bussiness then I want to add "usertype = 1" into my document and when personal then "usertype=2" I managed to automaticaly create a document on signup via functions

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp()

const db = admin.firestore()

exports.createUserData = functions.auth.user().onCreate((user) => {
const uid = user.uid
const email = user.email
const newUserRef = db.collection("user").doc(uid)

return newUserRef.set({
    userId: uid,
    email: email
})
});

但是我怎样才能将usertype"也传递给该函数呢?或者使用哪个函数来做到这一点?

But how I can I also pass "usertype" into that function? Or which function to use to do that?

推荐答案

functions.auth.user().onCreate(函数中没有传入额外的信息,所以无法区分有两种类型的用户.

No additional information is passed into the functions.auth.user().onCreate( function, so there's no way to distinguish between the two types of users there.

您可以做的最好的事情是在客户端中通过从那里编写相同的文档来设置用户类型.为防止出现竞争条件,您需要将用户文档的整个创建移至客户端.

The best you can do is set the user-type from within the client by writing the same document from there. To prevent a race condition, you'll want to move the entire creation of the user-document to the client.

这篇关于通过 Firestore 函数 onCreate 向用户配置文件添加其他数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆