Node js Query Async Map function is call

267 Views Asked by At
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);
                }
            })
        }

I don't get any response after each.save method call in the mapSeries method final callback.I am trying this solution.How i will do the same thing. How I resolve that and handle this kind of situation?

1

There are 1 best solutions below

0
On

I tried to simplify code, but I'm not sure that my code realizes your needs. Also I cann't test it :D
dataValue, each, element, finalResult are very common names, so you should use them with caution to keep code is readable/supportable.

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