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

前端 未结 2 566
予麋鹿
予麋鹿 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:50

    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
     
    });
    

提交回复
热议问题