0

I am trying yo create a constructor from another file to my project, the thing is, when y run my js only recognice the constructor if is declared in the same file, like this... This Works

function docWeb(){
 this.index = fs.readFileSync('./shalimar/index.html');
 this.userLogin = fs.readFileSync('./shalimar/home-shalimar-user.html');
 this.galery = fs.readFileSync('./shalimar/galeria.html');
 this.basket = fs.readFileSync('./shalimar/carrito.html');
 this.sells = fs.readFileSync('./shalimar/facturacion.html');
 this.upload = fs.readFileSync('./shalimar/upload.html');
}

var pagina = new docWeb();
 res.writeHead(200, { 'Content-Type': 'text/html' });
 res.write(pagina.userLogin);
 res.end();
 return;
  
  /*This Works*/

But when i try to take the constructor to another file

var mod=   require('./modulos/mod1');

var pagina = new mod.docWeb();
 res.writeHead(200, { 'Content-Type': 'text/html' });
 res.write(pagina.userLogin);
 res.end();
 return;
  
  
  /*mod1*/
  
  var fs =   require('fs');

exports.docWeb = () =>{
this.index =   fs.readFileSync('./shalimar/index.html');
this.userLogin =  fs.readFileSync('./shalimar/home-shalimar-user.html');
this.galery =   fs.readFileSync('./shalimar/galeria.html');
this.basket =   fs.readFileSync('./shalimar/carrito.html');
this.sells =   fs.readFileSync('./shalimar/facturacion.html');
this.upload =   fs.readFileSync('./shalimar/upload.html');
}

This throw me

TypeError: mod.docWeb is not a constructor
Eleazar Ortega
  • 509
  • 2
  • 7
  • 17
  • What does `./modulos/mod1.js` look like? – T.J. Crowder Nov 20 '17 at 16:35
  • in the second snipet you can see what mod1 looks like... After /*mod1*/ coment – Eleazar Ortega Nov 20 '17 at 18:06
  • 1
    Why did you not only move it to the other file, but also change it to arrow syntax? [Arrow functions are not constructors!](https://stackoverflow.com/q/34361379/1048572) Use the `function` keyword. – Bergi Nov 20 '17 at 18:24
  • Please don't use stack snippets for examples you cannot run in the browser (snippets have a "Run code snippet" button **for a reason**), and don't combine code from different files in the same code block. It makes it very difficult to help you. – T.J. Crowder Nov 20 '17 at 18:55

2 Answers2

1
exports.docWeb = docWeb();

docWeb() returns undefined, so you haven't actually exported anything.

You probably want to export the function itself.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

You need to change this:

exports.docWeb = docWeb();

to this:

exports.docWeb = docWeb;

You want to export the constructor function itself, not the result of executing it. What you were doing was calling the constructor and then exporting the return value. In Javascript, when you put () after a symbol that instructs the interpreter to execute the function right now. If you want to refer to just a reference to the function, you use the function name without ().

jfriend00
  • 683,504
  • 96
  • 985
  • 979