0
votes

How does one delete the last document in a collection through firebase functions trigger?

I am trying to delete a document that I don't have a reference to. I just want to delete the last document using order by the descending and limiting to 1.

admin.firestore().collection('notifications').orderBy("date",'desc').limit(1).delete();

This is the error I get in the functions log.

"TypeError: admin.firestore(...).collection(...).orderBy(...).limit(...).delete is not a function at exports.updateUser.functions.firestore.document.onUpdate (/srv/index.js:219:97) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:119:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)"

1

1 Answers

0
votes

admin.firestore().collection('notifications').orderBy("date",'desc').limit(1) returns a Query object. As you can see from the linked API documentation, Query does not have a method called delete.

You will have to get() the results of the query, look at the yielded QuerySnapshot to see if it matched a document, then delete the document using its reference.

You should go over the documentation for Cloud Firestore queries in order to get familiar with the APIs.