问题
Problem
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.
Data
// 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"}
]
Code
// 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))
Result
- 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.
Log
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
It's strange that in some section, user.email
and createdUser
are diffrent.
Help wanted
I'd like to know how to fix it. If possible, I'd like to know the causes too. Thank you!
回答1:
Just a guess - you are using the javascript SDK to create the user, not the admin SDK.
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.
Firebase docs
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(...);
})
})
Firebase admin reference
回答2:
I solved it by my self. It seems async
call in map made something wrong.
self = this
for (let i = 0; i < 5; i++) {
const self = this
await auth.createUserWithEmailAndPassword(data[i].email, 'password')
.then(function (res) {
const ref = usersRef.doc(res.user.uid)
self.data[i]._id = res.user.uid
ref.set(self.data[i])
}
)
}
来源:https://stackoverflow.com/questions/64675731/unable-to-add-data-to-custom-user-collection-properly-in-firestore