2

Given the code below, i'm assuming i'm running into an async issue.

exports.existingCheck = function (req, res, next) {

    var query = [],
        message = [],
        userObj = {},
        i, body = req.body;

    if(body.displayName){
        var regex = new RegExp(["^", body.displayName, "$"].join(""), "i");
    };

    if(req.user){
        var userObj = req.user;
    }

    if (body.displayName !== userObj.displayName) {
        console.log('body n no match')
        query.push({
            displayName: regex
        });
    }
    if (body.email !== userObj.email) {
        console.log('body e no match')

        query.push({
            email: body.email
        });
    }
    console.log('query pre ', query)

    if (query.length) {
        console.log('query init ', query)
        //find a match for email or display name and send appropriate error message;
        User.find({
                $or: query
            },
            function (err, existing) {
                if (err) {
                    console.log('register er ', err)
                }
                if (existing.length) {

                    for (i = 0; i < existing.length; i++) {

                        var conditional1 = false, conditional2 = false;

                        console.log('conditional1 init ', conditional1)

                        if(body.displayName && (userObj._id !== existing[i]._id)){
                            conditional1 = body.displayName.toLowerCase() === existing[i].displayName.toLowerCase();
                        };

                        console.log('conditional1 after ', conditional1)

                        if(body.email && (userObj._id !== existing[i]._id)){
                            conditional2 = body.email.toLowerCase() === existing[i].email.toLowerCase();
                        }

                        if (conditional2) {
                            message.push('Email is not unique.');
                        }

                        if (conditional1) {
                            message.push('Display name has already been taken.');
                        }
                    }
                }
            });
    }
    console.log('message check ', message)
    if (message.length) {
        return res.status(409).send({
            'message': message
        });
    }
    console.log('next')
    next();
};

the code below results in the console.logS firing in this order:

body n no match
query pre  [ { displayName: /^bobohead$/i } ]
query init  [ { displayName: /^bobohead$/i } ]
message check  []
next
conditional1 init  false
conditional1 after  true

With the problem being that the conditionals are not receiving their values until after the message check and next() are invoked.

I thought that the if statements were blocking code and that one would wait for the other to be fired.

I'm assuming i'll need to add an else statement to call a function that invokes the message check and next() & to invoke that same function at the end of the initial if statement.

do I also need to ensure i invoke next() within an else statement, to ensure that it is not called before the return in the message check has processed? :

console.log('message check ', message)
if (message.length) {
    return res.status(409).send({
        'message': message
    });
}
else{
  console.log('next')
  next();
 }
};
2
  • And what is the problem exactly? is it that console.log always logs { displayName: /^bobohead$/i }? Commented Feb 1, 2017 at 3:47
  • No, that the conditionals are not invoked before the next() or message / error check. Commented Feb 1, 2017 at 3:48

1 Answer 1

1

This looks like your call to User.find is asynchronous; thus, the code inside the callback function you pass gets executed after the containing existingCheck function returns.

If you want something to happen only after the User.find call finishes, you'll have to put that code inside the callback, too.

Note that you can't return a value from the enclosing function from inside a callback. After all, by the time the callback executes, the enclosing function has already finished. If you want to return an asynchronous value, return a Promise to it instead (or pass the value to a callback).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.