0

i am trying to delete a group of documents from a query using cloud functions. The cloud function is called and exits with status ok but the documents are not deleted. What could be the issue.

exports.deleteExpiredSellerPosts = functions.firestore
    .document('sellerPost/{post}')
    .onCreate(async(snapshot,context)=>{
    const now=snapshot.updateTime
    const post=snapshot.data();
    const seller=post.seller;
    const postsToDelete = await admin.firestore()
        .collection('sellerPost')
        .where("seller", "==" ,seller)
         .orderBy('expireTime','desc')
         .endAt([now])
    postsToDelete.get().then(function(querySnap){
       querySnap.forEach(function(doc){
           console.log(doc);
         doc.ref.delete();
       })
   })
   console.log('Success');
   return 0;
 })

I receive no error on console but documents are not deleted.

1
  • In addition to the points mentioned by Doug, are you sure that using .endAt([now]) in your query is giving the expected results? What do you get if you do const postsToDelete = admin.firestore().collection('sellerPost').where("seller", "==", seller).orderBy('expireTime','desc').endAt([now]); const snap = await postsToDelete.get(); console.log(snap.size); return null; Commented Dec 13, 2020 at 12:55

1 Answer 1

1

If you follow the documentation, you will find that you are obliged to return a promise that resolves only after all the async work is done for the function. Otherwise, Cloud Functions will shut things down that are not complete at the time the function returns.

You've got a few problems here. Firstly, you're using await with something that doesn't return a promise, which means it's not doing anything at all. Secondly, your use of then on the the promise returned by get() is not pausing the execution of the code to wait for the query to finish. Thirdly, you're completely ignoring the promises returned by each call to delete(). You will have to rewrite this to handle all the promises correctly.

Minimally, you should consider:

  • Using await on the call to get() pause the function and wait for the query to complete
  • Use a for loop (not forEach) to iterate the document resu;ts, and use await on each call to delete() to wait for each document to be deleted.
Sign up to request clarification or add additional context in comments.

2 Comments

I changed it to await postsToDelete.get().then(function(querySnap){ for(doc in querySnap){ doc.ref.delete(); } but it gives this error await is only valid in async function
Don't mix await with then. They serve the same purpose - use one or the other, not both. I strongly suggest taking some time to learn JavaScript more fully, as understand how async programming works is crucial to writing effective functions. Cloud Functions isn't really a great way to learn JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.