Skip to main content
1 de 2

A rota retorna "undefined" antes de função com await acabar

Eu tenho essa rota que faz uma chamada para o "teachersRoomsClassesController", que busca informações no banco e retorna o valor. Porém o "console.log(subjects)" está printando "undefined", e depois o "console.log(a)" no controller printa o valor esperado. Segue abaixo o código:

Rota:

router.get('/', authentication.required, async (req, res) => {

    const teacher_id = req.user.teacher.teacher_id;

    const subjects = await teachersRoomsClassesController.findAllByTeacherId(teacher_id);

    console.log(subjects);
    
    const courses = await coursesController.findAllToThirds();

    res.render('grade.html', { courses });
});

Controller:

async findAllByTeacherId(teacher_id: string){

        const teachersRoomsClassesService = new TeachersRoomsClassesService();
        const subjectsController = new SubjectsController();
        const roomsController = new RoomsController();

        const teacherClasses = await teachersRoomsClassesService.findAllByTeacherID(teacher_id);


        async function getAllInformation(){
            var teacherClassesResponse = [];
            for(const TC of teacherClasses){
                const subject = await subjectsController.fyndBySubjectId(TC.subject_id);
                const room = await roomsController.findById(TC.room_id);
                teacherClassesResponse.push({
                    subject: subject,
                    room: room
                });
            }

            return teacherClassesResponse;
        };

        getAllInformation()
        .then( a => {
                console.log(a);
                console.log("controller");
                return a;
            }
        );
    }

Logs do servidor:

query: 
        SELECT * FROM teachers_rooms_classes
        WHERE teachers_rooms_classes.teacher_id = "c4a2c4a1-9572-4b7c-a722-5249cd580b0d"
undefined **// console.log da rota**
query: SELECT cod, name FROM subjects
        WHERE subjects.id = "29c16137-51c5-4e90-b932-594ed8f7ec40"
query: 
        SELECT rooms.num, rooms.period, rooms.year
        FROM rooms
        WHERE rooms.id = "464a9848-643e-4b15-85a7-fcb51d905f31"
query: SELECT cod, name FROM subjects
        WHERE subjects.id = "29c16137-51c5-4e90-b932-594ed8f7ec40"
query:
        SELECT cod, name, info
        FROM courses
        ORDER BY name ASC;
query:
        SELECT rooms.num, rooms.period, rooms.year
        FROM rooms
        WHERE rooms.id = "5f16aabc-6914-463f-9726-4fb9fdaee29e"
query: SELECT cod, name FROM subjects
        WHERE subjects.id = "29c16137-51c5-4e90-b932-594ed8f7ec40"
query: 
        SELECT rooms.num, rooms.period, rooms.year
        FROM rooms
        WHERE rooms.id = "d3c87e5c-de79-4f5f-a45c-5b1cda05dd1c"
[ **// console.log do controller**
  {
    subject: TextRow { cod: '4863', name: 'ECONOMIA BRASILEIRA CONTEMPORANEA' },
    room: TextRow { num: '31', period: 'NOTURNO', year: '1°' }
  },
  {
    subject: TextRow { cod: '4863', name: 'ECONOMIA BRASILEIRA CONTEMPORANEA' },
    room: TextRow { num: '1', period: 'INTEGRAL', year: '1°' }
  },
  {
    subject: TextRow { cod: '4863', name: 'ECONOMIA BRASILEIRA CONTEMPORANEA' },
    room: TextRow { num: '32', period: 'NOTURNO', year: '1°' }
  }
]
controller