无法在Firestore中将数据正确添加到自定义用户集合 [英] Unable to add data to custom User collection properly in Firestore
问题描述
我想将几个用户数据添加到我自己同时创建的Firestore Authenticatedced用户列表和 users
集合中,但是效果不佳. users
集合仅会更新一部分.
I'd like to add several user data to firestore Authenticateced user list AND to users
collection which I created by myself at same time, but it does't go well. users
collection are updated only its part of it.
// javascript
users: [
{email: "n_0@example.com", username: "user0"},
{email: "n_1@example.com", username: "user1"},
{email: "n_2@example.com", username: "user2"},
{email: "n_3@example.com", username: "user3"},
{email: "n_4@example.com", username: "user4"}
]
代码
// javascript
import * as app from 'firebase/app'
import 'firebase/auth'
const config = JSON.parse(process.env.VUE_APP_FIREBASE_CONFIG)
app.initializeApp(config)
export const firebase = app
export const auth = app.auth()
function asyncCreateUser(user) {
return auth.createUserWithEmailAndPassword(
user.email,
'password'
).then(function (createdUser) {
console.log('---')
console.log('user.email', user.email)
console.log('createdUser', createdUser.user.email)
const ref = usersRef.doc(createdUser.user.uid)
return ref.set(user)
})
}
this.users.map(user => asyncCreateUser(user))
结果
- 经过身份验证的用户还可以.
-
users
集合有问题:集合中只有三个用户.在不同的执行方式中,添加到user
集合中的用户数量可能会有所不同. - Authenticated users are ok.
users
collection has problem: it has only three users in the collection. The number of users added touser
collection may differ in different execution.
Result
Debug.vue?2083:50 user.email n_3@example.com
Debug.vue?2083:51 createdUser n_2@example.com
Debug.vue?2083:49 ---
Debug.vue?2083:50 user.email n_2@example.com
Debug.vue?2083:51 createdUser n_1@example.com
Debug.vue?2083:49 ---
Debug.vue?2083:50 user.email n_1@example.com
Debug.vue?2083:51 createdUser n_1@example.com
Debug.vue?2083:49 ---
Debug.vue?2083:50 user.email n_0@example.com
Debug.vue?2083:51 createdUser n_0@example.com
Debug.vue?2083:49 ---
Debug.vue?2083:50 user.email n_4@example.com
Debug.vue?2083:51 createdUser n_4@example.com
奇怪的是,在某些部分, user.email
和 createdUser
是不同的.
It's strange that in some section, user.email
and createdUser
are diffrent.
我想知道如何解决它.如果可能的话,我也想知道原因.谢谢!
I'd like to know how to fix it. If possible, I'd like to know the causes too. Thank you!
推荐答案
只是一个猜测-您正在使用javascript SDK创建用户,而不是管理员SDK.
Just a guess - you are using the javascript SDK to create the user, not the admin SDK.
Javascript SDK在创建帐户后登录用户,因此基本上您可以连续5次快速登录和退出新帐户,因此在创建Firestore文档时会混淆用户ID-可能是您当时刚登出:
The Javascript SDK logs the user in after creating the account, so basically you rapidly logged a new account in and out 5 times in a row, hence the mix up with the user ids when creating the firestore documents - it can be that you were just logged out at that moment:
如果创建了新帐户,则用户将自动登录.请查看下面的下一步"部分以获取已登录的用户详细信息.
If the new account was created, the user is signed in automatically. Have a look at the Next steps section below to get the signed in user details.
如果要批量创建用户帐户,最好在安全的环境中使用admin SDK(例如
If you want to bulk-create user accounts you are better off using the admin SDK in a secure environment (e.g. cloud functions) and simply trigger the https function from your frontend. The way you are doing it now means that all the accounts will be created sequentially which can be quite time consuming when you create lots at once - if you are using a cloud function and the admin sdk you can kick off the creation of all accounts in parallel and return a promise once all are finished - something along the lines of:
return Promise.all(users.map(user => admin.auth().createUser(user)
.then(function(userRecord) {
return admin.firestore().collection('users').doc(userRecord.uid).set(...);
})
})
这篇关于无法在Firestore中将数据正确添加到自定义用户集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!