Reputation: 427
This question has already been answered some places, but none of the solutions worked for me. I also followed the official doc about this. and tried troubleshooting my code for a week, but my code seems correct, only the error keeps appearing that there is not such Object
I noticed that when I used storage triggers for thumb generations for the same image it working, and I compared the file path, bucket name, with the one I'm trying to delete with firestore triggers, there are the same. but only in firestore triggers this is ending in error No such Object
Here is my code and I don't see what is wrong with it.
'use strict';
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
//var serviceAccount = require("./xproject-cdfdc-firebase-adminsdk-xxxx-xxxxx.json");
/*admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xproject-cdfdc.firebaseio.com"
});*/
admin.initializeApp();
const db = admin.firestore();
const storage = admin.storage();
exports.cleanStorage = functions.firestore.document('items/{itemId}').onDelete((snapshot,context) => {
const id = snapshot.get('itemId');
//log: gettig id successfully
const imageName = snapshot.get('imageName');
//log: getting image name successfully
const defaultBucket = storage.bucket();
//log: defaultBucket.name() => xproject-cdfdc.appspot.com
//log: My bucket is initialzed successfully and exists:
const imageFile = defaultBucket.file('images/'+id+'/'+imageName);
//log: imageFile.name() =>images/W0qMLHqfQANfMsbUEmihK49NFgm2_1595320646264/photo1595320646264.jpg
//log: this path is correct and the file exists on stoarage
return imageFile.delete().then(result =>{
return console.log(`Deleted ${imageName}`);
}).catch(error => {
return console.log("Deliting failed: error->"+error);
//log: error->Error: No such object: xproject-cdfdc.appspot.com/images/W0qMLHqfQANfMsbUEmihK49NFgm2_1595320646264/photo1595320646264.jpg
});
});
So now I don't know what I should correct in my code as in stackoverflow all similar questions have similar solution I've already tried. Any help will be appreciated. thank you.
Upvotes: 6
Views: 2371
Reputation: 427
I finally solved my issue. Actually there were nothing wrong with my script and everything was working perfectly.
What happened is , before using cloud functions to automatically delete my storage image after the deletion of corresponding item in firestore, I had a function in android code that was doing all the job.
Now after writing my cloud functions, I forgot to edit my android app code to not delete the storage image.
So , the deletion from the android code were faster than the cloud functions and in the time the cloud functions completes the job, the file was not existing anymore.
So now I understood the expression ""we are only humain
this mistake cost me a whole week.
(Also deleting folder not working for me and get no such object. deleting files only working)
Upvotes: 4