I have a Database object (database.js) that looks like this:
//create the database
function Database(host, dbuser, dbpassword){
this.host = host;
this.dbuser = dbuser;
this.dbpassword = dbpassword;
this.connection = null;
}
Database.prototype = {
connection: function(){
this.connection = mysql.createConnection({
host: host,
user: dbuser
});
},
createDatabase: function(){...};
I'm then importing this object into my main app.js using a require statement
var db = require('./database.js');
However, when I try to construct my database object, I get a TypeError Database is not a constructor
var connection = new db('localhost','root');
connection.connection();
What am I doing wrong here? I read up on prototypes and it seems that I'm not lacking anything in that department so it seems that it has something to do with my require statement?
class. It will makes your code easier and you gonna avoid errors like this.Databasein your filedatabase.js?{host: host, user: dbuser}→{host: this.host, user: this.dbuser}