Save results to csv

parent a49dd9ee
......@@ -5,6 +5,7 @@
const mongoose = require("mongoose");
const User = mongoose.model('users');
const Results = mongoose.model('results');
const { Parser } = require("json2csv");
const fs = require('fs');
//const Paper = mongoose.model('paper');
......@@ -44,6 +45,41 @@ exports.results = async (req, res) => {
percentArray: req.body.percentArray,
}
var user = req.body.user;
var task = req.body.task;
var dataArray = req.body.dataArr;
var flattenedArrayFull = [];
for (let slide in dataArray) {
flattenedArray = dataArray[slide].map((x) => {
x['task']=task;
x['user']=user;
x['slide']=slide;
return x;
});
flattenedArrayFull = flattenedArrayFull.concat(flattenedArray);
}
console.log("========================")
console.log(flattenedArrayFull)
console.log("========================")
//csv file creation part start
const fields = ['task', 'user', 'slide', 'x', 'y', 'bb', 'type', 'timestamp'];
const json2csvParser = new Parser({ fields });
const csv = json2csvParser.parse(flattenedArrayFull);
const content = csv;
//fs.writeFile('/Users/INSIGHT/Desktop/research/vc/research-backend/results/'+req.body.task+'_'+req.body.user+'_'+Date.now()+'.csv', content, { flag: 'w+' }, function (err) {
fs.writeFile('./results/'+req.body.task+'_'+req.body.user+'_'+Date.now()+'.csv', content, { flag: 'w+' }, function (err) {
if (err) throw err;
console.log("It's saved!");
});
//csv file creation part end
// save results
await new Results(results).save((err, data) => {
if (err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment