简体   繁体   中英

Firestore document not deleted

I'm trying to delete a doc from Firestore, I don't get an error but the document is still in the database: simply as that, it keeps on being in the collection it belongs to.

The cloud function is:

exports.countdowns_crud = functions.https.onRequest((request, response) => {

    var req = request;
    var res = response;

    if (request.method === 'DELETE') {
        const countdownId = request.path.split('/')[1];

        const deleteOperation = db.DeleteCountdown(countdownId);

        if (!deleteOperation) {
            console.log('Delete operation result: ', deleteOperation);

            cors(req, res, () => {
                res.status(204).send("DELETED");
            });
        }
        else {
            console.error(addOperation);
            cors(req, res, () => {
                res.status(500).send("INTERNAL SERVER ERROR");
            });
        };

        return;
    }

    cors(req, res, () => {
        res.status(405).send("NOT ALLOWED");
        return;
    });

})

The DeleteCountdown function is in another module:

module.exports = {

  DeleteCountdown: (countdownId) => {
    const countdownsCollection = app.firestore.collection('countdowns');

    countdownsCollection.doc(countdownId).delete()
      .then((res) => {
        console.log('Result: ', res);
        return null;
      })
      .catch((e) => {
        console.error(`unable to delete the countdown ${countdowmnId}: ${e}`);
        return e;
      });
  }

}

This is the logic in a google cloud function, which it's correctly invoked by my react app upon deletion. The passed id is correct, no error is returned, but the doc keeps on living in the collection.

I had the same problem, no error and null returned (exactly the same when it works) because I forgot to set the rules to allow writes (or specifically deletions) directly in the Firebase console or by the firestore.rules files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM