Skip to content

Commit c900e0b

Browse files
authored
Merge pull request #3252 from Cookiezaurs/master-bugfix
[SER-149] DB Viewer cannot export data when data is filter is applied
2 parents bea2089 + 0818170 commit c900e0b

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

api/parts/data/exports.js

+22-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var exports = {},
1111
json2csv = require('json2csv'),
1212
json2xls = require('json2xls');
1313

14+
//const log = require('./../../utils/log.js')('core:export');
15+
1416
//npm install node-xlsx-stream !!!!!
1517
var xlsx = require("node-xlsx-stream");
1618
const Transform = require('stream').Transform;
@@ -316,7 +318,7 @@ function getValues(values, valuesMap, paramList, doc, options) {
316318
/**
317319
* Stream data as response
318320
* @param {params} params - params object
319-
* @param {Stream} stream - cursor stream
321+
* @param {Stream} stream - cursor object. Need to call stream on it.
320322
* @param {string} options - options object
321323
options.filename - name of the file to output to browser
322324
options.type - type to be used in content type
@@ -354,7 +356,7 @@ exports.stream = function(params, stream, options) {
354356
params.res.write(head.join(',') + '\r\n');
355357
}
356358

357-
stream.on('data', function(doc) {
359+
stream.stream(options.streamOptions).on('data', function(doc) {
358360
var values = [];
359361
var valuesMap = {};
360362
getValues(values, valuesMap, paramList, doc, {mapper: mapper, collectProp: listAtEnd}); // if we have list at end - then we don'thave projection
@@ -382,7 +384,7 @@ exports.stream = function(params, stream, options) {
382384
if (listAtEnd === false) {
383385
sheet.write(paramList);
384386
}
385-
stream.on('data', function(doc) {
387+
stream.stream(options.streamOptions).on('data', function(doc) {
386388
var values = [];
387389
var valuesMap = {};
388390
getValues(values, valuesMap, paramList, doc, {mapper: mapper, collectProp: listAtEnd});
@@ -400,7 +402,7 @@ exports.stream = function(params, stream, options) {
400402
else {
401403
params.res.write("[");
402404
var first = false;
403-
stream.on('data', function(doc) {
405+
stream.stream(options.streamOptions).on('data', function(doc) {
404406
if (!first) {
405407
first = true;
406408
params.res.write(doc);
@@ -478,30 +480,24 @@ exports.fromDatabase = function(options) {
478480
if (options.sort) {
479481
cursor.sort(options.sort);
480482
}
481-
if (options.limit) {
482-
cursor.limit(parseInt(options.limit));
483-
}
483+
484484
if (options.skip) {
485485
cursor.skip(parseInt(options.skip));
486486
}
487-
487+
if (options.limit) {
488+
cursor.limit(parseInt(options.limit));
489+
}
490+
options.streamOptions = {};
488491
if (options.type === "stream" || options.type === "json") {
489-
options.output = options.output || function(stream) {
490-
exports.stream(options.params, stream, options);
492+
options.streamOptions.transform = function(doc) {
493+
doc = transformValuesInObject(doc, options.mapper);
494+
return JSON.stringify(doc);
491495
};
492-
cursor.stream({
493-
transform: function(doc) {
494-
doc = transformValuesInObject(doc, options.mapper);
495-
return JSON.stringify(doc);
496-
}
497-
});
498-
options.output(cursor);
499496
}
500-
else if (options.type === "xls" || options.type === "xlsx" || options.type === "csv") {
497+
if (options.type === "stream" || options.type === "json" || options.type === "xls" || options.type === "xlsx" || options.type === "csv") {
501498
options.output = options.output || function(stream) {
502499
exports.stream(options.params, stream, options);
503500
};
504-
cursor.stream();
505501
options.output(cursor);
506502
}
507503
else {
@@ -593,19 +589,15 @@ exports.fromRequestQuery = function(options) {
593589
done(null, data);
594590
}
595591
});
592+
options.streamOptions = {};
596593
if (options.type === "stream" || options.type === "json") {
597-
cursor.stream({
598-
transform: function(doc) {
599-
doc = transformValuesInObject(doc, options.mapper);
600-
return JSON.stringify(doc);
601-
}
602-
});
603-
exports.stream({res: outputStream}, cursor, options);
604-
}
605-
else if (options.type === "xls" || options.type === "xlsx" || options.type === "csv") {
606-
cursor.stream();
607-
exports.stream({res: outputStream}, cursor, options);
594+
options.streamOptions.transform = function(doc) {
595+
doc = transformValuesInObject(doc, options.mapper);
596+
return JSON.stringify(doc);
597+
};
608598
}
599+
exports.stream({res: outputStream}, cursor, options);
600+
609601
options.output(outputStream);
610602
}
611603
}

plugins/dbviewer/frontend/public/javascripts/countly.views.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@
242242
},
243243
preparedProjectionFields: function() {
244244
var ob = {};
245-
for (var i = 0; i < this.projection.length; i++) {
246-
ob[this.projection[i]] = 1;
245+
if (this.projection && Array.isArray(this.projection)) {
246+
for (var i = 0; i < this.projection.length; i++) {
247+
ob[this.projection[i]] = 1;
248+
}
247249
}
248250
return ob;
249251
},

0 commit comments

Comments
 (0)