之后我确实上传了文件,我将它们的数据保存在 mongo 中,但是如果我处理一个文件,它就可以工作,但是 loop/forEach/map/for 无论如何都不工作
文件循环
await Promise.all(files.map(async (file) => {
let url= await uploadFileAndSave(req, file);
await saveFileDB(file,url);
} ))
上传文件功能承诺
return new Promise(async (resolve, reject) => {
let pathDir = req.params.uId + "/";
let typeFolder = getTypeFolder(file.name);
console.log("type", typeFolder);
if (typeFolder) {
pathDir += req.body.app ? req.body.app + "/" : "";
pathDir += req.body.appId ? req.body.appId + "/" : "";
pathDir += typeFolder + "/";
currentPath = path.join(__dirname, "../uploads/", pathDir, Date.now() + "__" + file.name)
console.log("currentPath", currentPath);
file.mv(currentPath).then(async () => {
console.log("sAAaved!!!", file.name);
resolve(currentPath)
})
}
})
}
保存数据库功能
saveFileDB = async (file, pathUrl) => {
return new Promise(async(resolve, reject) => {
let name = pathUrl.slice(pathUrl.lastIndexOf('/') + 1, pathUrl.length)
let newFile = new fileModel({
name,
url: pathUrl,
dateCreated: Date.now(),
size: file.size / 1024 / 1024,
icon: iconsClasses[file.name.split(".")[1]],
delete: false,
});
// console.log("newFile",newFile);
try {
let result = await newFile.save();
console.log("save in db", result);
} catch (err) {
console.log(err);
res.send(err);
}
newFile.save((err, fileSaved) => {
if (err){
console.log("err",err);
reject(err)}
console.log('fileSaved',fileSaved);
resolve(fileSaved)
});
})
}
冉冉说
相关分类