Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据导出到CSV文件NodejS Express_Javascript_Node.js_Mongodb_Csv_Mongoose - Fatal编程技术网

Javascript 将数据导出到CSV文件NodejS Express

Javascript 将数据导出到CSV文件NodejS Express,javascript,node.js,mongodb,csv,mongoose,Javascript,Node.js,Mongodb,Csv,Mongoose,我正在尝试将不同类型的数据从数据库导出到Nodejs和express中的CSV文件。到目前为止,我已经尝试了几个库,但由于各种原因,没有任何一个像我预期的那样有效 我怎样才能解决这个问题?为了能够将我想要的所有数据导出到CSV文件,我应该知道什么?我怎样才能强迫我的浏览器去做呢 谢谢所以,在经历了很多努力之后,我将分享我的主要见解,这些见解对于那些在web开发中迈出第一步的人来说并不那么明显 导出为CSV可分为两个主要步骤: 1.将数据整理到CSV结构/模型中。 2.导出数据/使其在客户端下载

我正在尝试将不同类型的数据从数据库导出到Nodejs和express中的CSV文件。到目前为止,我已经尝试了几个库,但由于各种原因,没有任何一个像我预期的那样有效

我怎样才能解决这个问题?为了能够将我想要的所有数据导出到CSV文件,我应该知道什么?我怎样才能强迫我的浏览器去做呢


谢谢

所以,在经历了很多努力之后,我将分享我的主要见解,这些见解对于那些在web开发中迈出第一步的人来说并不那么明显

导出为CSV可分为两个主要步骤: 1.将数据整理到CSV结构/模型中。 2.导出数据/使其在客户端下载

所以我要把它分解。 第一步-将数据排列到CSV结构/模型中: 要将数据转换为CSV结构,很可能您可以找到一个库,该库将要导出的数据转换为CSV格式。 如果您的数据模型和我的一样复杂,那么您必须创建一个自定义函数。不管怎样,这都不会太复杂。 我使用的此类函数的一个示例:

// The function gets a list of objects ('dataList' arg), each one would be a single row in the future-to-be CSV file
// The headers to the columns would be sent in an array ('headers' args). It is taken as the second arg
function dataToCSV(dataList,headers){
    var allObjects = [];
    // Pushing the headers, as the first arr in the 2-dimensional array 'allObjects' would be the first row
    allObjects.push(headers);

    //Now iterating through the list and build up an array that contains the data of every object in the list, in the same order of the headers
    dataList.forEach(function(object){
        var arr = [];
        arr.push(object.id);
        arr.push(object.term);
        arr.push(object.Date);

        // Adding the array as additional element to the 2-dimensional array. It will evantually be converted to a single row
        allObjects.push(arr)
    });

   // Initializing the output in a new variable 'csvContent'
    var csvContent = "";

    // The code below takes two-dimensional array and converts it to be strctured as CSV
    // *** It can be taken apart from the function, if all you need is to convert an array to CSV
    allObjects.forEach(function(infoArray, index){
      var dataString = infoArray.join(",");
      csvContent += index < allObjects.length ? dataString+ "\n" : dataString;
    }); 

    // Returning the CSV output
    return csvContent;
}
总之,, 我写这篇文章是为了让其他人不会像我一样在使用nodejs和express导出CSV时遇到困难。 如果您发现任何错误,或者您认为上面写的一些内容应该更彻底地解释,请让我知道,我将进行必要的更改


问候。

您考虑过使用mongoexport命令吗?下面是一个简单的步骤:根据您请求文件的方式,您可能还需要下载响应。你可以使用这个软件包,也可以在源代码中插入你的脑袋,然后自己写。今天我只写了30行,所以非常感谢你提供的详细信息answer@sale108,如果当时@user1149244-不需要大数据,会发生什么。显然,如果它太大而无法放入内存,那么您需要使用其他方法,可能是流式传输并在接收器端重新组装
//this statement tells the browser what type of data is supposed to download and force it to download
    res.writeHead(200, {
        'Content-Type': 'text/csv',
        'Content-Disposition': 'attachment; filename=*custom_name*.csv'
    });
// whereas this part is in charge of telling what data should be parsed and be downloaded
    res.end(dataToCSV(dataList,["ID","Name","Date"]),"binary");