0

I've been stuck on this issue for a few days now and it's driving me insane. I have a route that retrieves available employees from a Mongo Database. When this route is triggered, all of the necessary functions are fired, however, what happens next, I don't understand.

It looks like either the function returns too early and the array of available employees is empty. Or, the route just gets triggered again. This is based on the output of the console in my express application:

Note: isAvailableAtTime is an asynchronous helper for isAvailable and they are getting fired 4 times because there are 4 employees in my DB at the moment.

Web server running on port 5000
Database connected successfully
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
No available employees
Function fired.
GET /schedule/findAvailableEmployees 304 81.615 ms - -
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false

I have all of my awaits in try catch blocks and none of them are throwing errors. Here is the code in the route definition:

router.get('/findAvailableEmployees', async(req, res) => {
    const availableEmployees = [];
    const start = req.body.start;
    const end = req.body.end;
    try{
        let employees = await Employee.find({});

        employees.forEach(employee => {
            let cur_employee = employee['first_name'];
            isAvailable(employee, start, end)
            .then(res => {
                if(res === true){
                    console.log("pushing ", employee['first_name']);
                    availableEmployees.push(employee);
                }
            })
            .catch(err => {
                console.log("Error in isAvailable: ", err);
            });
        });
        
        // await sleep(4000);

        if(availableEmployees.length < 1){
            console.log("No available employees");
            let none = {
                _id: 0,
                first_name: "None"
            };
            availableEmployees.push(none);
        } 

        console.log("Function fired.");
        res.status(200).json({
            message: "Request to /findAvailableEmployees was successful.",
            availableEmployees: availableEmployees
        });

    }
    catch(error){
        console.log("Error in /findAvailableEmployees", error);
        res.status(424).json({
            message: "Request to /findAvailableEmployees failed.",
            err: error
        });
    }
    
});

The results get produced correctly and the availableEmployee array gets populated correctly after the "second" call to this route (GET /schedule/findAvailableEmployees 304 81.615 ms - -) But does not get returned. The browser receives an empty availableEmployee array (No available employees).

EDIT: Node output after changing method back to POST:

Web server running on port 5000
Database connected successfully
OPTIONS /schedule/findAvailableEmployees 200 4.523 ms - 4
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
isAvailable Fired
isAvailableAtTime Fired
No available employees
Function fired.
POST /schedule/findAvailableEmployees 200 83.073 ms - 117
Overlapping!
isAvailableAtTime Finished - available:  false
isAvailable Finished - result: false
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: true
pushing  Siman
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false
isAvailableAtTime Finished - available:  true
isAvailable Finished - result: false
wawaloo_17
  • 109
  • 2
  • 9
  • Why is a get request accessing the `req.body` method? – destroyer22719 Feb 09 '21 at 03:52
  • It was originally a post method. Did some digging on this same issue and found that cors might be the culprit since I saw that the OPTION method was getting called automatically. I switched to a get method to see if it would make a difference (it didn't) and I forgot to switch it back to a post method. I also tried adding `app.use(cors)`, the only thing that seemed to do is prevent OPTION being automatically triggered. I don't think cors is the problem here, but I could be wrong. – wawaloo_17 Feb 09 '21 at 04:25

0 Answers0