Cloud functions onUpdate: Cannot read property 'forEach' of undefined

前端 未结 2 565
予麋鹿
予麋鹿 2021-01-29 04:21

Now I am trying to update the picture in my project. I could update the picture url in the cloud fire store. But also I want to delete the previous picture from the cloud storag

2条回答
  •  醉梦人生
    2021-01-29 04:28

    Independently of the forEach problem, your code cannot work: you try to pass an URL to the file() method of a Bucket, while you should pass the name of the file in this bucket.

    One solution would be to save, in another field of the Product doc, the name of the file.

    Then, as Cleanbeans explained, you don't need to use forEach, since in your Cloud Function, you are treating only one Firestore document.

    Just use the other field containing the file name and adapt Cleanbeans' solution as follows:

    exports.onProductUpdate = functions.firestore.document('Product/{productId}').onUpdate(async(snap, context) => {
        const deleteFileName = snap.before.data().fileName;
        const bucket = admin.storage().bucket();
    
        await bucket.file(deleteFileName).delete())
    
        return null;   // Don't forget to return null for example (or an Object or a Promise), to indicate to the platform that the CF can be cleaned up. See https://firebase.google.com/docs/functions/terminate-functions
    
    });
    

提交回复
热议问题