Node . js查询异步映射函数是调用


function(dataValue, cb) {
            req.app.db.models.User.find({
                _id: { $ne: dataValue._id }
            }, function(err, totalUser) {
                if (!err) {
                    var len = totalUser.length;
                    if (len !== 0) {
                        req.app.utility.async.map(totalUser, function(each, callback) {
                            console.log(each);
                            req.app.utility.async.mapSeries(each.nonregisterContact, function(element, callback1) {
                                console.log('element', element.number);
                                console.log('dataValue', dataValue.mobileNumber);
                                console.log('kolka', Number(element.number) === Number(dataValue.mobileNumber));
                                if (Number(element.number) === Number(dataValue.mobileNumber)) {
                                    each.registerContact.push(dataValue._id.toString());
                                    each.nonregisterContact.splice(element, 1);
                                    each.save(function(err, finalResult) {
                                        if (!err) {
                                        } else {
                                            console.log(err);
                                        }
                                    })
                                    callback1(null, null);
                                } else {
                                    callback1(null, null);
                                }
                            }, function(err, final) {
                                if (!err) {
                                    callback(null, null);
                                } else {
                                    console.log(err);
                                }
                            });
                        }, function(err, result) {
                            if (!err) {
                                console.log('2');
                                return cb(null, dataValue);
                            } else {
                                console.log(err);
                            }
                        });
                    } else {
                        return cb(null, dataValue);
                    }
                } else {
                    cb(err);
                }
            })
        }

我每次都没有得到任何回应。在mapSeries方法的最终回调中调用save方法。我正在尝试这个解决方案。我也会做同样的事。我如何解决这个问题,如何处理这种情况?

我试图简化代码,但我不确定我的代码是否实现了您的需求。我也不能测试它:D
dataValue, each, element, finalResult是非常常见的名称,因此您应该谨慎使用它们,以保持代码的可读性/可支持性。

// very bad idea is include other libraries to app
var async = require('async');
var db = require('db'); // this module must export connection to db
...
function (dataValue, cb) {
    // processUser use data from closure of current function => inside of current
    function processUser (user, callback) {
        async.mapSeries(user.nonregisterContact, function(contact, callback){
            // Check and exit if condition is not satisfied. It's more readable.
            if (Number(contact.number) !== Number(dataValue.mobileNumber)) 
                return callback(null); // ignore user
            user.registerContact.push(dataValue._id.toString());
            user.nonregisterContact.splice(contact, 1);
            user.save(function(err, finalResult) { // Is finalResult ignore?
                if (err)
                    console.log(err);
                callback(); // ingnore error
            })
    }, callback);
    db.models.User.find({_id: { $ne: dataValue._id }}, function(err, userList) {
        if (!err) 
            return cb(err);
        if (userList.length == 0)
            return cb(new Error('Users not found'));
        // use named function to avoid stairs of {}
        async.map(userList, processUser, cb);
    })
};

相关内容

  • 没有找到相关文章

最新更新