0

I'm using javascript to fill a variable, but i'm having a problem getting the data back.

here is the code:

var invoice_email = function(req, res, clientObject, staffId, appointment, staff_template, client_template, callback){
    //This step sets up all the variables in the locals object
    var locals = [];

    //This makes the query if StaffId has no email property
            if (staffId[0].email !== undefined){
                console.log('StaffID is an Object');
                locals.staff = staffId[0];
            } else {
                var myStaff_id = staffId[0].toString();
                User.findOne({roles: 'staff', _id: myStaff_id})
                    .select('_id email displayName profile_pic')
                    .exec(function(err, user) {
                        if (err) {
                            console.log(err);/*res.status(400).send({
                                message: errorHandler.getErrorMessage(err)
                            });*/
                        } else {
                            locals.staff = user;

                        }
                });
            }
            console.log(locals.length);
            console.log(locals);
};

I'm trying to get the 'locals' variable outside of the if-else statement but can't get it to return the locals variable. I know the function cycles, and then executes the if-else statement, but cant get the locals variable out, which is pretty annoying.

I've also tried using return locals, but this wont do, as I'm calling the invoie_email function from another function and want to do more stuff in here before i return to the caller. Can anyone help?

Mobaz
  • 597
  • 3
  • 10
  • 26

0 Answers0