Browse Source

Refactor the code to load the project path from the config/constants.js file and fix a bug in the parse method where it returns false instead of rejecting the promise with an error message when columns are invalid

node16
Onja 12 months ago
parent
commit
6db619d3c4
  1. 3
      src/app.js
  2. 3
      src/config/constants.js
  3. 11
      src/models/file.js

3
src/app.js

@ -8,9 +8,6 @@ var indexRouter = require('./routes/index');
var app = express(); var app = express();
// Declare a global variable to hold project path
global.__basedir = __dirname;
// view engine setup // view engine setup
app.set('views', path.join(__dirname, 'views')); app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');

3
src/config/constants.js

@ -0,0 +1,3 @@
module.exports = {
basedir: __dirname,
};

11
src/models/file.js

@ -1,3 +1,4 @@
/* global basedir */
const path = require('path'); const path = require('path');
const URL = require("url"); const URL = require("url");
const fs = require('fs'); const fs = require('fs');
@ -12,7 +13,10 @@ const { PassThrough } = require('stream');
const slugify = require('slugify'); const slugify = require('slugify');
const e = require('express'); const e = require('express');
const dest = path.join(appRoot, 'public/csv'); // Load project path from config/constants.js
const { basedir } = require('../config/constants');
const dest = path.join(basedir, 'public/csv');
// Create a generateFilePath function witch returns a path with a filename and datetime // Create a generateFilePath function witch returns a path with a filename and datetime
@ -116,12 +120,13 @@ class File extends EventEmitter {
// create a parse method which read the file and return a stream // create a parse method which read the file and return a stream
async parse(columns) { parse(columns) {
const stream = new PassThrough(); const stream = new PassThrough();
// check if columns is valid // check if columns is valid
if (!columns || columns.length) { if (!columns || columns.length) {
return Promise.reject(new Error('Invalid columns')); // return Promise.reject(new Error('Invalid columns'));
return false;
} }
// Create a variable to hold csv columns indexes // Create a variable to hold csv columns indexes

Loading…
Cancel
Save