我要删除用户,并且使用multer包,该用户拥有个人资料图片以及存储在公用文件夹中的许多其他图片。
现在我要按照以下代码片段删除个人资料照片,它仅适用于一个附件,如果我需要删除许多附件怎么办?
let index = await currentSubChapter.subChapterDetail.indexOf(detailId);
if (index !== -1) {
await currentSubChapter.subChapterDetail.splice(index, 1);
await currentSubChapter.save();
}
let attachmentstodelete = response.attachments;
for (let index = 0; index < attachmentstodelete.length; index++) {
const attachment = attachmentstodelete[index];
let indexofAttachment = attachment.indexOf('/public/uploads');
let path = attachment.substr(indexofAttachment);
await unlinkAsync(`.${path}`);
}
return res.status(200).json({
message: 'Deleted Successfully',
});
}