Use a Controller to connect routes with asynchronous functions [Node]

Asked

Viewed 218 times

2

Good night!

I’m having doubts about something that seems easy, but I’m not getting out of place. The idea would be to utilize the GET method to recover users from the database, but would like to use MVC for this. Following the MVC concept in theory, my route would call my Controller, and it would call my asynchronous function in the Model. If I call directly my asynchronous function on the route by the Model it works, but I’m not able to make the Controller "intermediate".

My route:

Here I left only two routes configured for testing, and they would call the controller below.

const express = require('express');
const UsersController = require('./controllers/UsersController');
const AccountController = require('./controllers/AccountController');
const routes = express.Router();

routes.get('/users', UsersController.testeIndex);
routes.post('/users', UsersController.testeCreate);
//routes.delete('/users/:id_user', UsersController.deletetUsers);

//routes.get('/account', AccountController.index);
//routes.post('/account', AccountController.create);

module.exports = routes;

My controller:

Here the idea would be to have the controller just call the model function and return the information to the route. (In this case would successfully search the user in the database)

const UserDAO = require('../models/database/UsersDAO');

module.exports = {
    testeIndex : async function(req, res) {
        var index = await UserDAO.index;
        return res.json(index);

    },
    testeCreate : async function(req, res) {
        var create = await UserDAO.create;
        return res.json(console.log(create));
    }
};

My model (in this case, is a DAO):

If on the route I call directly this file and the functions of it works perfectly, but I believe that it runs away from the concept of MVC

const connection = require('./connection');

module.exports = {
    async index(request, response) {
        const users = await connection('users').select('*');
        return (response.json(users));
    },

    async create(request, response) {
        const { usuario, senha } = request.body;
        //Testar criptografia mais tarde
        await connection('users').insert({
            usuario,
            senha
        });
        return response.json({usuario, senha});
    },
}

I have no return in Insomnia when I use GET, no error in the project

Não tenho retorno no Insomnia quando uso o GET, nem erro no projeto

  • 1

    When you do var index = await UserDAO.index, you are assigning the method reference UserDAO.index to the variable index. If you want to perform the function, you must invoke it. For this, do var index = await UserDAO.index().

  • 1

    In addition to what has already been said, I recommend taking a look at this module: https://www.npmjs.com/package/express-async-handler

  • 1

    Besides... the DAO shouldn’t get the req and res, only the data necessary for the action in the database (registration, alteration, etc)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.