javascript nodejs如果语句以意外顺序被调用

新手

给定下面的代码,我假设我遇到了一个异步问题。

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();
};

下面的代码将按以下顺序触发console.logS:

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

问题在于条件只有在调用消息check和next()之后才接收它们的值。

我认为if语句正在阻塞代码,并且其中一个将等待其他语句被触发。

我假设我需要添加一个else语句来调用一个函数,该函数调用消息检查,而next()&则在初始if语句的末尾调用相同的函数。

我还需要确保我在else语句中调用next(),以确保在消息检查中的返回被处理之前没有被调用吗?

console.log('message check ', message)
if (message.length) {
    return res.status(409).send({
        'message': message
    });
}
else{
  console.log('next')
  next();
 }
};
x

看来您的呼叫User.find是异步的;因此,您所传递的回调函数内部的代码将在包含existingCheck函数返回后执行

如果您希望仅在User.find调用完成后才能发生某些事情,则也必须将该代码放入回调中。

请注意,您不能从回调函数中的封闭函数返回值。毕竟,在执行回调时,封闭函数已经完成。如果要返回异步值,请改为返回一个异步值Promise(或将该值传递给回调)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章