Thursday 4 May 2017

Callback was already called bluebird

Can anyone pls explain why is this happening?? Unhandled rejection Error: Callback was already called. It works if createBulkUsers accepts array of single object for array of multiple objects I am getting this issue.

function createBulkUsers(users){
            return new Promise(function(resolve, reject) {
                if(!users){
                    reject('No users data provided');
                }else{
                    var createdAt = currentTimeStamp();
                    var verifyMeUserModel = Subscriber.app.models.verifyme_user;
                    verifyMeUserModel.validatesUniquenessOf('email');
                    verifyMeUserModel.validatesUniquenessOf('phone');

                    var existingUserEmails = [];
                    var allUsers = [];

                    async.map(users,
                        function(vmUser,cb){
                            verifyMeUserModel.create({
                                "username": vmUser.name,
                                "email": vmUser.email,
                                "password": generateRandomPassword(),
                                "phone": vmUser.phone,
                                "role": vmUser.relation.toUpperCase(),
                                "profileImageUrl": vmUser.picture || '',
                                "created_at": createdAt
                            },
                            function(err,_user){
                                if(err){
                                    if(err.name === 'ValidationError'){
                                        console.log('VM-User Already Exists');
                                        existingUserEmails.push(_user.email);
                                    }else{
                                        throw err;
                                    }
                                    cb(null,_user);
                                }else{
                                    console.log('VM-User created');
                                    console.log(_user);
                                    cb(null,_user);
                                }
                            })
                        },
                        function(error,createdVmUsers){
                            if(error){
                                return reject(error);
                            }
                            console.log('existingUserEmails',JSON.stringify(existingUserEmails));
                            allUsers = createdVmUsers.slice();

                            //filtering all existing users
                            createdVmUsers = createdVmUsers.reduce(function(accumulator,user,index){
                                if(user.verifyme_user_id && user.verifyme_user_id.length) { accumulator.push(user); }
                                return accumulator; // *********  Important ******
                            },
                            []);

                            console.log('createdVmUsers',JSON.stringify(createdVmUsers));

                            //sending emails to newly created users
                            sendBulkEmails(createdVmUsers);

                            if(existingUserEmails.length > 0){
                                console.log('there are existing users');
                                //when users exists for provided emails or phones
                                //fetching all existing users from db
                                var cypher = {
                                    query: 'MATCH (u:verifyme_user) WHERE u.email IN {users} RETURN u',
                                    params: {
                                        users: existingUserEmails
                                    }
                                }
                                neoResults(cypher, {}, function(result){
                                    if(result){
                                        //update verifyme_user_id of existing user in allUsers array
                                        //we are doing this b'coz relation which needs to be created is i.e. role prop is stored in that array
                                        result.map((__user) => {
                                            __user = __user.u.properties;
                                            var index = _.findIndex(allUsers, function(o) { return ( o.email === __user.email || o.phone === __user.phone ) });
                                            if(index > -1) { allUsers[index].verifyme_user_id = __user.verifyme_user_id; }
                                        });

                                        var identityModel = Subscriber.app.models.identity;
                                        identityModel.validatesUniquenessOf('email');
                                        identityModel.validatesUniquenessOf('phone');
                                        identityModel.validatesUniquenessOf('facebook_email');
                                        identityModel.validatesUniquenessOf('linkedIn_email');
                                        identityModel.validatesUniquenessOf('google_email');

                                        async.each(
                                            allUsers,
                                            function(_createdUser, cbk) {

                                                identityModel.create({
                                                    "name": _createdUser.username,
                                                    "email": _createdUser.email,
                                                    "phone": _createdUser.phone,
                                                    "profileImageUrl": _createdUser.profileImageUrl || '',
                                                    "facebook_email": _createdUser.email,
                                                    "linkedIn_email": _createdUser.email,
                                                    "google_email": _createdUser.email,
                                                    "created_at": _createdUser.createdAt
                                                },
                                                function(error,_identity){
                                                    if(error){
                                                        if(error.name === 'ValidationError'){
                                                            console.log('Identity Already Exists');
                                                            cypher = {
                                                                query: 'MATCH (u:verifyme_user {verifyme_user_id:"'+ _createdUser.verifyme_user_id +'"}) MATCH (ido:identity) WHERE ( ido.email="'+_createdUser.email+'" OR ido.phone='+_createdUser.phone+' OR ido.facebook_email="'+_createdUser.email+'" OR ido.linkedIn_email="'+_createdUser.email+'" OR ido.google_email="'+_createdUser.email+'" ) CREATE UNIQUE (u)<-[r:RELATED_TO]-(ido) RETURN ido',
                                                                params: {}
                                                            };
                                                            runNeoQuery(cypher,{},function(res){
                                                                if(res){
                                                                    console.log('Rel betn ido & u created');
                                                                }else{
                                                                    console.log('Rel betn ido & u create failed');
                                                                }
                                                            })
                                                        }
                                                        cbk();
                                                    }else{
                                                        //identity created
                                                        console.log('New Identity Created');
                                                        if(_identity.profileImageUrl && _identity.profileImageUrl.length){ indexImageForFacialRecog(_identity.id,_identity.profileImageUrl); }
                                                        cypher = {
                                                            query: 'MATCH (u:verifyme_user {verifyme_user_id:"'+ _createdUser.verifyme_user_id +'"}), (ii:identity {id:"'+ _identity.id +'"}) CREATE (u)<-[r:RELATED_TO]-(ii) RETURN ii',
                                                            params: {}
                                                        }
                                                        runNeoQuery(cypher, {}, function(res){
                                                            if(res){
                                                                console.log('Rel created');
                                                            }else{
                                                                console.log('Rel create failed')
                                                            }
                                                        });
                                                        cbk();
                                                    }
                                                });
                                            },
                                            function(error) {
                                                if (error) {
                                                    throw new Error(error);
                                                    reject(error);
                                                } else {
                                                    console.log('identities created successfully');
                                                    resolve({
                                                        users: allUsers,
                                                        identities: []
                                                    });//here is the error
                                                }
                                            }
                                        );
                                    }else{
                                        reject('Users not found');
                                    }
                                });
                            }else{
                                //when all users are newly created

                                var identityModel = Subscriber.app.models.identity;
                                identityModel.validatesUniquenessOf('email');
                                identityModel.validatesUniquenessOf('phone');
                                identityModel.validatesUniquenessOf('facebook_email');
                                identityModel.validatesUniquenessOf('linkedIn_email');
                                identityModel.validatesUniquenessOf('google_email');

                                async.each(
                                    allUsers,
                                    function(_createdUser, cbk) {

                                        identityModel.create({
                                            "name": _createdUser.username,
                                            "email": _createdUser.email,
                                            "phone": _createdUser.phone,
                                            "profileImageUrl": _createdUser.profileImageUrl,
                                            "facebook_email": _createdUser.email,
                                            "linkedIn_email": _createdUser.email,
                                            "google_email": _createdUser.email,
                                            "created_at": _createdUser.createdAt
                                        },
                                        function(error,_identity){
                                            if(error){
                                                if(error.name === 'ValidationError'){
                                                    console.log('Identity Already Exists');
                                                    cypher = {
                                                        query: 'MATCH (u:verifyme_user {verifyme_user_id:"'+ _createdUser.verifyme_user_id +'"}) MATCH (ido:identity) WHERE ( ido.email="'+_createdUser.email+'" OR ido.phone='+_createdUser.phone+' OR ido.facebook_email="'+_createdUser.email+'" OR ido.linkedIn_email="'+_createdUser.email+'" OR ido.google_email="'+_createdUser.email+'" ) CREATE UNIQUE (u)<-[r:RELATED_TO]-(ido) RETURN ido',
                                                        params: {}
                                                    };
                                                    console.log('cypher',cypher);
                                                    runNeoQuery(cypher,{},function(res){
                                                        if(res){
                                                            console.log('Rel betn ido & u created');
                                                        }else{
                                                            console.log('Rel betn ido & u create failed');
                                                        }
                                                    });
                                                }else{
                                                    throw error;
                                                }
                                                cbk();
                                            }else{
                                                //identity created
                                                console.log('New Identity Created');
                                                if(_identity.profileImageUrl && _identity.profileImageUrl.length){ indexImageForFacialRecog(_identity.id,_identity.profileImageUrl); }
                                                cypher = {
                                                    query: 'MATCH (u:verifyme_user {verifyme_user_id:"'+ _createdUser.verifyme_user_id +'"}), (ii:identity {id:"'+ _identity.id +'"}) CREATE (u)<-[r:RELATED_TO]-(ii) RETURN ii',
                                                    params: {}
                                                }
                                                runNeoQuery(cypher, {}, function(res){
                                                    if(res){
                                                        console.log('Rel created')
                                                    }else{
                                                        console.log('Rel create failed');
                                                    }
                                                });
                                                cbk();
                                            }
                                        });
                                    },
                                    function(error) {
                                        if (error) {
                                            throw new Error(error);
                                            reject(error);
                                        } else {
                                            console.log('identities ----- created successfully');
                                            resolve({
                                                users: allUsers,
                                                identities: []
                                            });//here is the error
                                        }
                                    }
                                );
                            }
                        });
                }
            });
        }



via Pawan Wagh

No comments:

Post a Comment