我正在编写一个功能来删除我的应用程序中的用户,该功能由NodeJS Mongoose和Cloudinary提供支持,我想删除用户已上传到其个人文件夹和文件夹本身的所有图片。这是我编写的用于删除单个用户的代码(请注意,用户名是其文件夹的名称):
module.exports = (id, callback) => {
User.findByIdAndDelete(id, function (err, user) {
if (err)
return callback(err);
if (!user)
return callback(null, null);
cloudinary.api.delete_resources_by_prefix(`${user.username}/`, function (err) {
if (err && err.http_code !== 404) {
return callback(err);
}
cloudinary.api.delete_folder(`${user.username}`, function (error, result) {
if (error && error.http_code !== 404) {
return callback(error);
}
return callback(null, `${user.username}`);
});
});
});
};
问题是,当我运行它时,第二个API请求答复此错误:
{ message: 'Folder is not empty', http_code: 400 }
这显然是不正确的,因为我已经删除了上面API调用中的文件。我还通过cloudinary的UI检查了第一个调用的正确行为,除了最后一个调用之外,其他所有操作都正常。所以我要问的是: