INVALID_ARGUMENT: 一次调用中不能写入超过 500 个实体

INVALID_ARGUMENT: cannot write more than 500 entities in a single call

我想根据客户要求在 firestore 中存储 500 多个文档。我在向 firebase 写入(设置)文档时遇到了同样的问题。经过对网络的大量研究,我创建了自己的解决方案。

希望对你有所帮助!

此代码用于创建 5000 个虚拟记录。

const trailContacts = () => {
  let names = [];
  for (let index = 1; index < 5002; index++) {
    names.push({
      name: 'trail',
    });
  }
  return names
};

这是将名称存储到 firebase-firestore 的代码。

let contacts = trailContacts();
let count = Math.ceil(contacts.length/500);
for (let index =0;  index < count; index++) {
  let splicedContacts = contacts.splice(0, 499);
  let contactsBatch = db.batch();
  splicedContacts.forEach((con) => {
    let collection = {};
    let name = con.name;
    let collectionRef = db.collection('trail');
    contactsBatch.set(collectionRef, collection);
  });
  await contactsBatch.commit();
}