批量用户创建后 Firebase 不添加文档

Firebase doesn't add documents after bulk user creation

背景

我正在尝试使用网络应用程序中的初始用户列表为我的 Firebase 播种。这应该是一个两步过程:

代码

// Initialize Firebase:

var db = firebase.initializeApp(config).firestore();

// Function to add a new user:

function add(email) {
  firebase.auth().createUserWithEmailAndPassword(email, 'password')
    .then(function (user) {
      console.log('User created:', email, user.user.uid);
      return db.collection('users').add({
        email: email,
        uid: user.user.uid
      });
    })
    .then(function () {
      console.log('Document created:', email);
    })
    .catch(function (error) {
      console.error('Could not create user/document:', error);
    });
}

// Seed database with random users:

for (var i = 0; i < 10; ++i) {
  add(i + '@foo.bar');
}

问题

上一节中的代码不起作用。我的期望是它应该创建 10 个用户,并在成功时将 10 个相应的文档添加到数据库中。但是,即使正确创建了用户,它也只会将部分文档 (1-3) 添加到数据库

想法

我想出了一些关于此代码不起作用的可能原因的想法:

以下是 Firebase 支持回答的摘录:

Each time a new user is created with Auth, it sets the newly created user as the current user of the application. Whenever Firestore detects that a user change has occurred, it closes all outstanding streams and cancels any requests in flight.

这完全有道理并解释了行为。