3
Hello. I am building an application with nodejs and I need to authenticate users by registering a new user and logging into the application. I’m using consign to perform autoload of my scripts, as you can see below
consign().include('app/routes').then('app/models').then('app/controllers').into(app);
My authentication class (Autenticacaodao.js) is inside the directory models and implemented as follows:
class AutenticacaoDAO {
constructor() {
this._instance;
}
static getStance() {
if (!this._instance) {
this._instance = new AutenticacaoDAO();
}
return this._instance;
}
// Demais métodos
}
module.exports = function () {
return AutenticacaoDAO;
}
As you can see, a static method that generates a new instance. When I try to access this method in my route appears the following error: Typeerror: Class constructor Autenticacaodao cannot be Invoked without 'new'
module.exports.home = function (application, req, res) {
var auth = application.app.models.AutenticacaoDAO().getInstance();
}
The Node accuses error that I need a new to invoke the class, but since I am accessing a static method that returns the instance, not the need to instate the object. However, this same error does not occur when access class by require:
module.exports.home = function (application, req, res) {
var auth = require('../models/AutenticacaoDAO')().getStance();
}
I would like to know how to access this method using the consign (since I don’t want to change the structure of the project). I’ve searched other forums for the answer to my problem but could not find the solution. Could anyone help me? if I could arrange the project in another way? I am new in Noode js and Avascript, so I still get lost in some concepts and any help will be welcome.
But then if it’s a method
estático
the call shouldn’t be like thisapplication.app.models.AutenticacaoDAO.getInstance();
?– LeAndrade
Yes, I thought the same thing. I made the method flame as you said, but now the following error occurs: Typeerror: application.app.models.AutenticaDAO.getInstance is not a Function
– Marcos R. Morais