Skip to content

TypeError: this.AuthService is not a constructor #50

@gfkauer

Description

@gfkauer

Dada a classe:

class UsersController {
    constructor(User, AuthService) {
        this.User = User;
        this.AuthService = AuthService;
    }

    async authenticate(req, res) {
        const authService = new this.AuthService(this.User);
        const user = await authService.authenticate(req.body);
        if (!user) {
            return res.sendStatus(401);
        }
        const token = this.AuthService.generateToken({
            name: user.name,
            email: user.email,
            password: user.password,
            role: user.role
        });
        return res.send({ token });
    }
}

export default UsersController;

os testes unitários apresentam o erro:

Controller: Users
       login
         should return 401 when the user can not be found:
     TypeError: this.AuthService is not a constructor
      at UsersController.authenticate (src/controllers/users.js:69:29)
      at Context.<anonymous> (test/unit/controllers/users_spec.js:298:35)
      at processImmediate (node:internal/timers:464:21)

para ver o codigo completo atual pode verificar em:
https://github.com/gfkauer/to-do-api

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions