为什么在使用archiver.file模块压缩文件时出现“队列关闭错误”

时间:2019-03-05 11:18:59

标签: node.js

我正在尝试使用存档器在nodejs中以zip格式下载多个文件。这是我的代码:

exports.downloadAllFiles = function(req,res){
    var archive = archiver('zip', {
        gzip: true,
        zlib: { level: 9 } // Sets the compression level.
    });
    var output = fs.createWriteStream( "/home/files/Downloads/demo.zip");

    archive.pipe(output);

    demoDb.findOne({ caseguid: req.params.id }, function(err, data) { 
        if (err) {
            res.json(HttpStatus.INTERNAL_SERVER_ERROR, {});
        } else {
            if(data){
                data.Files.forEach(function(singleDoc){
                       archive.append(fs.createReadStream(singleDoc.filePath), { name: singleDoc.fileName })

                })

            }

         }

    })
    archive.finalize();
};

这是错误堆栈:

{ Error: queue closed
    at Archiver.append (/home/node_modules/archiver/lib/core.js:552:24)
    at Promise.<anonymous> (/home/server/controllers/caseController.js:1722:25)
    at Promise.<anonymous> (/home/node_modules/mpromise/lib/promise.js:177:8)
    at emitOne (events.js:96:13)
    at Promise.emit (events.js:188:7)
    at Promise.emit (/home/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/home/node_modules/mpromise/lib/promise.js:97:20)
    at /home/node_modules/mongoose/lib/query.js:1406:13
    at model.Document.init (/home/node_modules/mongoose/lib/document.js:254:11)
    at completeOne (/home/node_modules/mongoose/lib/query.js:1404:10)
    at Immediate.cb (/home/node_modules/mongoose/lib/query.js:1158:11)
    at Immediate.<anonymous> (/homenode_modules/mquery/lib/utils.js:137:16)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5) message: 'queue closed', code: 'QUEUECLOSED', data: undefined }

1 个答案:

答案 0 :(得分:2)

在完成回调之前,可能正在执行archive.finalize()行。尝试将您的archive.finalize移至回调内部。