【发布时间】:2018-09-22 14:17:29
【问题描述】:
我想用 mongoose 从我们的 mongodb 数据库中的 gridfs 获取图标 PNGS。然后应该压缩这些图标并在特定路线上提供服务。
我目前的代码如下:
var zip = require("node-native-zip");
async function getZipFile() {
//get the events out of the DB
db.Category.find({}).populate('icons.file').exec(async function (err, cats) {
if (err) {
//oh oh something went wrong, better pass the error along
return ({
"success": "false",
message: err
});
}
else {
//all good, build the message and return
try {
const result = await buildZip(cats);
return ({
"success": "true",
message: result
});
}
catch (err) {
console.log("ZIP Build Failed")
}
}
});
}
async function buildZip(cats) {
let archive = new zip();
for (let i = 0; i < cats.length; i++) {
cats[i].icons.forEach(function (icon) {
if (icon.size === "3x") {
db.Attachment.readById(icon.file._id, function (err, buffer) {
if (err)
return;
archive.add(cats[i]._id + ".png", buffer);
});
}
});
//return when everything is done
if (i === cats.length - 1) {
return archive.toBuffer();
}
}
}
module.exports =
{
run: getZipFile
};
我不想在运行前构建 zip,因为我想根据类别 ID 重命名图标。我尝试使用 async/await 结构,但我的回调在 zip 文件的构建开始之前就被返回了。
我正在调用函数
case 'categoryZip':
categoryHelper.getZipFile.run().then((result) => {
callback(result);
});
break;
这应该(据我理解)在压缩完成时触发回调,但我认为我在这里遗漏了一些重要的东西。
【问题讨论】:
-
基于回调的 API 不能在
async函数中“正常工作”。您必须将它们转换为承诺。这里的async函数不知道您的db方法中的回调,也不会await它们。 -
@PatrickRoberts 所以你说我应该将
db.Category.find({}).populate('icons.file').exec转换为基于承诺的函数,等待它,然后继续我的异步 zip 构建? -
是的,那个和
db.Attachment.readById(),虽然那个可能不能在forEach()中工作,但是你可以使用for (const icon of cats[i].icons)
标签: javascript node.js express mongoose async-await