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

五迷三道 提交于 2020-11-29 21:25:37

问题


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 storage using firebase cloud functions.

What I want to achieve is, to delete the previous picture from the cloud storage when I upload the new picture.

This is my data structure.

I have "sample" field in "Product" collection. When the picture in "sample" field is updated, I want to delete the original picture in the cloud storage.

But I got an error in the cloud functions log console.

TypeError: Cannot read property 'forEach' of undefined

This is my cloud functions code.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const Firestore = admin.firestore;
const db = Firestore();



exports.onProductUpdate = functions.firestore.document('Product/{productId}').onUpdate(async(snap, context) => {
    const deletePost = snap.before.data().sample;

    let deletePromises = [];
    const bucket = admin.storage().bucket();

    deletePost.images.forEach(image => {
        deletePromises.push(bucket.file(image).delete())
    });
    
    await Promise.all(deletePromises)
})

I want to fix this error.


回答1:


onUpdate is only looking at one document and it would appear from your screenshot of your doc that snap.before.data().sample is a string where your code is treating it like an object, or even a query snapshot?

Unless I've misunderstood, does this is correct your code?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const Firestore = admin.firestore;
const db = Firestore();



exports.onProductUpdate = functions.firestore.document('Product/{productId}').onUpdate(async(snap, context) => {
    const deletePost = snap.before.data().sample;
    const bucket = admin.storage().bucket();

    await bucket.file(deletePost).delete();

    return null;   // See https://firebase.google.com/docs/functions/terminate-functions
 
});



回答2:


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

});


来源:https://stackoverflow.com/questions/64858814/cloud-functions-onupdate-cannot-read-property-foreach-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!