'Node.js Unable to send multiple csv in an archive folder

I'have currently a problem to create and to send an archive from a node.js server.

In fact, i succeeded create an archive with 5 csv files on the archive but when i send the archive to my React Application and when i unzip this archive i'have got just one file ....

I have checked (local) the archive created by the server and there are 5 files instead of one when i unzip the folder send to the React Application.

My http response :

import { Parser } from "json2csv";
import archiver from "archiver";
import fs from "fs";
import path from "path";

const output = fs.createWriteStream(`csv/${campaign.title}.zip`, 'utf-8');

const archive = archiver("zip", {
zlib: { level: 9 }, // Sets the compression level.
});

output.on("close", function () {
console.log(archive.pointer() + " total bytes");
console.log("archiver has been finalized and the output file descriptor has closed.");
});

output.on("end", function () {
console.log("Data has been drained");
});

archive.on("warning", function (err) {
if (err.code === "ENOENT") {
    // log warning
} else {
    // throw error
    throw err;
}});

archive.on("error", function (err) {
throw err;
});

archive.pipe(output);

d.map(items => {
let file = items;
archive.append(file, { name: file });
});


archive.pipe(res)

archive.finalize();

res.setHeader('application/zip')

res.setHeader("Content-Disposition", `attachment; filename=${campaign.title}.zip`);

res.status(200).send(archive);


Solution 1:[1]

What is content of mystical d? You use archiver incorrectly I think:

your code:

d.map(items => {
let file = items;
archive.append(file, { name: file });
});

but documentation say: https://www.archiverjs.com/docs/archiver#append

append(source, data) ? {this}

Appends an input source (text string, buffer, or stream) to the instance.

When the instance has received, processed, and emitted the input, the entry event is fired.

Parameters

source - Buffer | Stream | String - The input source.
data - Object - The entry data.

Check content and use of d - you need data and filename for use in append

EDIT:

Don't use Array.map like Array.forEach, it's Anti-pattern: Is performing a mapping operation without using returned value an antipattern?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zbyso