Please help us, i am stuck why the following code snippet work fine locally 
but not on Herok. Actually problem is in callbacks they are not executing 
in the sequence as expected.

 

    
// Opration hook to send push notification
        GroupMember.observe('after save', function(ctx, next) {
            if(ctx.instance.joinStatus != 'self') {
                GroupMember.app.models.Group.findOne({
                    where: { id: ctx.instance.groupId},
                },
                function(err, group) { 
                    if (!err){
                        var d = new Date();
                        var notId = d.getTime(); // unique notification id
                        var message = new gcm.Message();
                        
                        message.addData('notId', notId);
                        message.addData('page', 'groupDetail');
                        message.addData('param', 'groupId');
                        message.addData('paramVal', ctx.instance.groupId);
                        
                        if(ctx.instance.joinStatus == 'pending') {  // 
Sending notification to group creator
                            console.log(1);
                            message.addData('title', 'New group membership 
request');
                            message.addData('message', 'An user requested 
to join your group');
                            GroupMember.app.models.Push.sendNotification(
group.createdBy, message);
                            
                        }else if(ctx.instance.joinStatus == 'accepted') {   // 
Sending notification to requested user 
                        
                            message.addData('title', 'Accepted group join 
request');
                            message.addData('message', 'Your request to 
join group has been accepted');
                            GroupMember.app.models.Push.sendNotification(ctx
.instance.userId, message);
                            
                        }else if(ctx.instance.joinStatus == 'rejected') {   // 
Sending notificati to requested user 
                        
                            message.addData('title', 'Rejected group 
membership request');
                            message.addData('message', 'Your request to 
join group has been rejected');
                            GroupMember.app.models.Push.sendNotification(ctx
.instance.userId, message);
                        }
                    }
                });
            }
            next();
        });

In the above code snippet i am calling *sendNotification()* method whenever 
a group member is created or updated.

    
module.exports = function(Push) {
        Push.sendNotification = function(userIds, message) {
            console.log(2);
            Push.app.models.Installation.getDeviceToken(userIds, function (
err, deviceIds) {
                console.log('returnd from instalation');
                if (!err) {
                    console.log(deviceIds);
                    var apiKey   = config.gcmServerApiKey;
                    var service  = new gcm.Sender(apiKey);
                   
                    service.send(message, { registrationTokens: deviceIds }, 
function (err, response) {
                        if(err) {
                            //console.log(deviceIds);
                            //console.error(err);
                        } else {
                            //console.log(deviceIds);
                            //console.log(response);
                        }
                    });
                }
            });
        };
    };

Now here i am calling *getDeviceToken()* function of *Installation *model 
which code snippet is as following.

    
module.exports = function(Installation) {
        Installation.getDeviceToken = function(userIds, cb) {
            console.log(3);
            var userIdsArr = [];
            if(!Array.isArray(userIds)) {
                userIdsArr.push(userIds);
            } else {
                userIdsArr = userIds;
            }
            Installation.find({
                where: { 
                    userId: {inq: userIdsArr} 
                },
                scope: {
                  fields: {deviceToken: true}
                }
            },
            function(err, result) { 
                if (!err) {
                    console.log(4);
                    var deviceTokens = [];
                    var stringifiedTokens = JSON.stringify(result);
                    var tokensObject = JSON.parse(stringifiedTokens);
                    /*tokensObject.forEach(function(token) {
                        deviceTokens.push(token.deviceToken);
                    });*/
                    
                    async.each(tokensObject, function (token, callback) {
                        deviceTokens.push(5);
                        callback();
                    }, function (err) {
                        if (err) { throw err; }
                        cb(null, deviceTokens);
                    });
                    
                }else{
                    console.log(err);
                }
            });
        };
    };

The problem is callbacks are not waiting for others callbacks to be 
finished and for their response and i am not getting results as expected.
If i run same code on local machine its working fine but not on Heroku.
Please help me where i am doing wrong.

Thank you :) 

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Heroku" group.

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/heroku?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"Heroku Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to