0

I want to delete two entries: one in database (Firestore), the other in storage (Cloud Storage). The following code should fulfill this goal.

However, in the logs of Cloud Functions, these lines are shown:

> 1st line: Function execution started

> 2nd line: Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

> 3rd line: Function execution took 821 ms, finished with status code: 200

> 4th line: Unhandled rejection

> 5th line: Error: Unable to delete the image.
    \n at /srv/index.js:59:9
    \n at <anonymous>
    \n at process._tickDomainCallback (internal/process/next_tick.js:229:7)

I think the error that is shown in line 2 causes the "errors" displayed in lines 4 and 5.

I have Spark Plan (the free billing account) and normally, all Google networks should be accessible. The image I'm trying to delete, and the database entry, are stored in Google servers! So I'm not using any "External network", thus my script actually should work (correct me if I'm mistaken).

I don't think I've done any error in my code.

Here it is:

const request2 = require('request');
const {Storage} = require('@google-cloud/storage');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase)
const storage = new Storage();
const admin_firestore = admin.firestore();

exports.deletePost = functions.https.onCall((data, context) => {

    if(!context.auth) {
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
    }

    const the_post = data.the_post;
    const filePath = context.auth.uid + '/posts/' + the_post;
    const promise_1 = storage.bucket('android-framrk-fxxx.appspot.com').file(filePath).delete();
    promise_1.then(function() {
        const promise_2 = admin_firestore.collection('users').document(context.auth.uid).collection('posts').document(the_post).delete();
        return promise_2.then(function() {
            return 1;
        }).catch(function() {
            throw new functions.https.HttpsError('unknown', 'Unable to delete the post.');
        });
    }).catch(function() {
        throw new functions.https.HttpsError('unknown', 'Unable to delete the image.');
    }); 
});

My question

Am I correct? i.e.: is it a bug from the Google devs team? Or did I make a mistake in my code that I didn't see?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70
  • 1
    Does this answer your question? [Cloud Functions for Firebase - Billing account not configured](https://stackoverflow.com/questions/42784135/cloud-functions-for-firebase-billing-account-not-configured) – Deniss T. Dec 12 '19 at 11:06
  • 1
    The warning about not having a billing account is always logged, so might not be the cause. But your code is hiding the actual cause. Change your `catch` handlers to log the error to learn more about the actual problem. So something like `}).catch(function(error) { console.log(error); throw new functions.https.HttpsError('unknown', 'Unable to delete the image.');` – Frank van Puffelen Dec 12 '19 at 14:30

1 Answers1

1

The message about billing is unrelated to the error that follows. You are just not dealing with promises correctly. You need to return a promise that resolves with the data to send to the client. Right now, your function is returning nothing, which means the async work you're starting might not complete, or complete with strange errors. Please review the documentation about that.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441