Yoh*_*ezu 0 javascript node.js google-cloud-storage firebase google-cloud-functions
现在我正在尝试更新我项目中的图片。我可以更新云火商店中的图片网址。但我也想使用 firebase 云功能从云存储中删除上一张图片。
我想要实现的是,当我上传新图片时,将之前的图片从云存储中删除。
我在“产品”集合中有“样本”字段。当“样本”字段中的图片更新时,我想删除云存储中的原始图片。
但是我在云功能日志控制台中遇到错误。
类型错误:无法读取未定义的属性“forEach”
这是我的云函数代码。
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)
})
Run Code Online (Sandbox Code Playgroud)
我想修复这个错误。
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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |