Cannot save after deleting collection in Firestore Cloud

69 Views Asked by At

I'm using Cloud Firestore and I got a strange issue: cannot save after deleting collection.

// DELETE ALL  COLLECTIONS 
const _feedback_delete_all_collections = await deleteAllCollections(user_id);
if(!_feedback_delete_all_collections) return _feedback_delete_all_collections;
            
// START SYNC, STORE AND RFM CALC
return await shopify._start(10, options, user_id);

I used for "deleteAllCollections" a Google suggested codes:

async function deleteCollection(_collection, _doc_id, _subcollection, batchSize = 50) {
 try {
    const collectionRef = this.db.collection(_collection).doc(_doc_id).collection(_subcollection);
    const query = collectionRef.limit(batchSize);        
    
    return new Promise((resolve, reject) => {
        this.deleteQueryBatch(query, resolve).catch(reject);
    });

  } catch (error) {
    return error;
 }
}

async function deleteQueryBatch(query, resolve) {
  const snapshot = await query.get();

  const batchSize = snapshot.size;
  if (batchSize === 0) {
    // When there are no documents left, we are done
    resolve(true);
  }

  // Delete documents in a batch
  const batch = this.db.batch();
  snapshot.docs.forEach((doc) => {
    batch.delete(doc.ref);
  });
  await batch.commit();

  // Recurse on the next process tick, to avoid
  // exploding the stack.
  process.nextTick(() => {
    deleteQueryBatch(query, resolve);
  });
}

All codes run sequentially perfect, and all feedback referred for writing are:

WriteResult {
  _writeTime: Timestamp { _seconds: 1601105237, _nanoseconds: 375427000 } }

I'm struggling with this issue.

Anyone did encountered before or know how to solve it?

Thank you in advance!

Edit - Shell Dump

Server listening on port 3000
---- START IMPORT index.js - _start
_feedback_delete_all_order  true
_feedback_delete_all_XXX  true
_feedback_delete_all_YYY  true
_feedback_delete_all_product  true
_feedback_delete_all_customer  true
---------- Shopify.js - _start 
_saveAllData - START  shopify
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 241293000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 242116000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 242197000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 242888000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 242988000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 246281000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 245678000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 249575000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 251043000 } }
Feedback Save Order:  WriteResult {
  _writeTime: Timestamp { _seconds: 1601135004, _nanoseconds: 251268000 } }
_saveAllData - END
1

There are 1 best solutions below

1
On

The function deleteQueryBatch signals that it is finished by calling resolve() without any argument, so you can await it, but it returns nothing (we say that it returns Promise<void>).
To detect the successful end of the deleteQueryBatch, you should wrap it in a try/catch as this:

try {
    await deleteAllCollections(user_id);
    return await shopify._start(10, options, user_id);
}
catch(error){
    // If deleteAllCollections fails, you will arrive here
    // before executing shopify._start
}

To learn more, I suggest reading this doc on Promises.