通过onCreate的Firestore函数将其他数据添加到用户个人资料 [英] Add additional data to user profile via firestore functions onCreate

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

问题描述

当用户通过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.

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

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