Firebase Batch Write fails with [Error: [firestore/invalid-argument]]

When I try to batch insert records in intervals of 450 (I chose this to make sure I was below the 500 firestore limit) I get an error for an invalid argument. Below is the exact code I am using.

I am using firestore within react-native with the below dependencies and versions:

    "@react-native-firebase/app": "^19.1.0",
    "@react-native-firebase/firestore": "^19.1.0",
    "react-native": "^0.73.5",

I am inserting records into a sub collection that does not yet exist. user_tracks is the top level collection that exists and the document id is a record indicator that is just used to link the top and sub collection. The document id corresponds to an existing document id under the users collection and finally the sub collection tracks is what I am ultimately trying to insert into.

export const updateUserLikedSongs = async (documentId, likedSongs) => {

  // Batch write 500 at a time
  console.log(`There are ${likedSongs.length} songs to insert.`);

  if (likedSongs.length <= 0) {
    console.log("No more documents to insert.");
    return true;
  }

  const batchSize = 450; // Firestore batch size limit
  var batch = firestore().batch();

  likedSongs.slice(0, batchSize).forEach((song) => {
    const docRef = firestore().collection("user_tracks").doc(documentId).collection("tracks").doc();
    batch.set(docRef, song); // Use song directly, no need for Object.assign
  });

  try {
    await batch.commit();
    console.log(`Successfully wrote ${batchSize} records.`);

    // Recursively call updateUserLikedSongs with the remaining songs
    return updateUserLikedSongs(documentId, likedSongs.slice(batchSize));
  } catch (error) {
    console.error("Error while batch writing:", error);
    throw error; // Rethrow the error to be caught by the caller
  }
};

I am able to loop through my liked songs array and insert them into the sub collection 1 at a time, however this seems extremely in-efficient and I would like some help to figure out what I am doing wrong with the batch insert. This method also throws a warning because of the number of promises waiting to resolve.

export const updateUserLikedSongs = async (documentId, likedSongs) => {

  likedSongs = likedSongs.slice(0, 750)
  // Batch write 500 at a time
  console.log(`There are ${likedSongs.length} songs to insert.`);

  const batchSize = 499; // Firestore batch size limit

  const batches = [];
  for (let i = 0; i < likedSongs.length; i += batchSize) {
    const batch = likedSongs.slice(i, i + batchSize);
    const batchPromises = batch.map(async (track) => {
      const docRef = firestore()
        .collection(`user_tracks/${documentId}/tracks`)
        .doc();
      await docRef.set(track); // Set each record in Firestore
    });
    batches.push(Promise.all(batchPromises));
  }

  try {
    // Wait for all batches to complete
    await Promise.all(batches);
    console.log("All records created successfully.");
  } catch (error) {
    console.error("Error creating records:", error);
  }
};

I have tried this on android and ios through react-native though I think that doesn’t matter since the javascript code and dependencies are ultimately the same.

Please offer some tips and guidance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật