I am trying to delete elements from 2 different collections in firestore using a cloud function. I am doing:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { GoogleAuth } = require("googleapis-common");
const { google } = require("googleapis");
const firebase_tools = require("firebase-tools");
const firestore = admin.initializeApp().firestore();
const Buffer = require('buffer');
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
const PROJECT_NAME = `projects/${PROJECT_ID}`;
exports.removeFromShoppingList = functions.region('europe-west1').https.onCall(async(snap, context) => {
const db = firestore;
const batch = db.batch;
const shoppingCartId = snap.shoppingCartId;
const shoppingListId = snap.shoppingListId;
const productId = snap.productId;
try {
if (!(context.auth && context.auth.token)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const pathShoppingListDetailsCollection = db.collection('shoppingListDetails').doc(shoppingListId)
.collection('products').doc(productId);
batch.delete(pathShoppingListDetailsCollection);
const pathShoppingListsCollection = db.collection('shoppingLists').doc(shoppingCartId)
.collection('shoppingListCart').doc(shoppingListId).collection('products').doc(productId);
batch.delete(pathShoppingListsCollection);
await batch.commit();
} catch(err){
functionCalledToHandleError();
}
})
And in the client in flutter I call the cloud function like this:
Future<void> removeProductsFromShoppingList({
required String shoppingCartId,
required String shoppingListId,
required String productId
}) async {
try {
final data = {
'shoppingCartId': shoppingCartId,
'shoppingListId' : shoppingListId,
'productId' : productId
};
var delete = functions.httpsCallable('removeFromShoppingList')
.call(data);
} on FirebaseFunctionsException catch (err){
print('estamos en el FirebaseFunctionsException ');
} catch (e) {
throw CouldNotDeleteNoteException();
}
}
And I am getting an error when I do the batch.delete(pathShoppingListDetailsCollection)
when I go check the logs in the google cloud console the only message that is helping is the textPayload that says textPayload: "There was an error deleting the documents"
is there a way to see this logs more in detail. Because with only this information I don't know the reason why I cannot delete the documents.
Thank you in advance.
CodePudding user response:
As @Frank van Puffelen said with the log err I got some more information about the error and I could solve the problem, thank you so much.
In my case the error was that batch.delete
was not a function and that was because I missed parentheses when declaring the const batch = db.batch()