2 回答

TA貢獻1886條經驗 獲得超2個贊
onUpdate 只查看一個文檔,從您的文檔屏幕截圖中可以看出,它snap.before.data().sample是一個字符串,您的代碼將其視為一個對象,甚至是一個查詢快照?
除非我誤解了,否則這是正確的您的代碼嗎?
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
});

TA貢獻1820條經驗 獲得超9個贊
無論問題如何forEach
,您的代碼都無法工作:您嘗試將 URL 傳遞給file()
Bucket 的方法,而您應該傳遞此存儲桶中的文件名稱。
Product一種解決方案是將文件的名稱保存在文檔的另一個字段中。
然后,正如 Cleanbeans 所解釋的,您不需要使用forEach,因為在您的 Cloud Function 中,您只處理一個 Firestore 文檔。
只需使用包含文件名的其他字段并調整 Cleanbeans 的解決方案,如下所示:
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
});
添加回答
舉報