I have this really weird issue where my Promise's .then is not working. The error is below:
TypeError: Cannot read property 'then' of undefined
The reason I call this weird is because I am using the same structure everywhere else and it works flawlessly. I would also like to add that the actual action it is supposed to do - does get done! So this is purely an issue with the way my Promises are "coded" - just syntactic sugar.
My code snippet is as below:
var updateAllUnreadNotifications = function(){
    userModel.aggregate([
        {$match:{"profileID": 123}},
        {$unwind: "$notifications"},
        {$match:{"notifications.read": false}},
        {$group: {_id:"$_id", notifications: {$push:"$notifications"}}}
        ], function(err, notifications){
             var notificationPromises = notifications.map(function(notification) {
                return new Promise(function(resolve){
                    userModel.findOneAndUpdate(
                        {profileID: 123, "notifications.id": notification.id},
                        {"notifications.$.read": true},
                        {safe: true},
                        function(err, result) {
                            if (err){
                               return;
                            } else if (result){
                                resolve(notification);
                            }
                        }
                    );
                });
            });
            return Promise.all(notificationPromises);
        });
};
updateAllUnreadNotifications()
.then(function(notifications){
    res.json({
        message: 'All is well'
    });
});
Furthermore, I have done a console.log on the notificationPromises and it does look like a chain of Promises. And of course, the result variable is also there so this is not an issue with the DB command.
What have I messed up? I am sure it will be a very obvious one but for the life of me, I can't isolate it.
Let me know if some more information is required.
 
     
    