Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise don't return anythings #36

Open
ChandanShankar opened this issue Nov 23, 2017 · 1 comment
Open

Promise don't return anythings #36

ChandanShankar opened this issue Nov 23, 2017 · 1 comment

Comments

@ChandanShankar
Copy link

Code find and create list of events, if not found. I am facing issue with the function findEvents don't return anything, Can you please help me to resolve.

service.js

.factory('Events', function($log, $q, $http, $rootScope,$cordovaCalendar) {
 
    findEvents = function(events) {
        var deferred = $q.defer();
        var dftlyEvents = events;
        /*
        Logic is:
        For each, see if it exists an event.
        */
        var promises = [];
        dftlyEvents.forEach(function(ev) {
            promises.push($cordovaCalendar.findEvent({
                title:ev.title,
                startDate:ev.startDate,
            }));
        });
        
        $q.all(promises).then(function(results) {
            console.log("in the all done"+results);   
            //should be the same len as events
            for(var i=0;i<results.length;i++) {
                dftlyEvents[i].status = results[i].length === 1;
            }
            deferred.resolve(dftlyEvents);
        });
        return deferred.promise;
    }

    createEvents = function(events) {
        console.log("createEvent"+JSON.stringify(events));
        var deferred = $q.defer();
        var dftlyEvents = events;
        /*
        Logic is:
        For each, see if it exists an event.
        */
        var promises = [];
        dftlyEvents.forEach(function(ev) {
            if(typeof(ev.status) != 'undefined' && !ev.status){
                //console.log("for"+JSON.stringify(ev));
                promises.push($cordovaCalendar.createEvent({
                    title:ev.title,
                    startDate:ev.startDate,
                    endDate: ev.endDate
                }));
            }
        });
        
        $q.all(promises).then(function(results) {
            console.log(results);   
            //should be the same len as events
            for(var i=0;i<results.length;i++) {
                dftlyEvents[i].created = results[i].length === 1;
            }
            deferred.resolve(dftlyEvents);
        });
        return deferred.promise;
    }
    
  return {
        get:findEvents,
        add:createEvents
  };

});

controller.js

Events.get(response).then(function(events) {
            $scope.events = events;
            Events.add($scope.events).then(function(result) {
              console.log("result");
            },function(error){
               console.log(" error at function create"+ JSON.stringify(error));
            }).catch(function (error){
               console.log("error at create catch"+ JSON.stringify(error));
            });
          },function(error){
            console.log("error at Event find "+ JSON.stringify(error));
          }).catch(function (error){
            console.log("catch error"+ JSON.stringify(error));
          });
@cfjedimaster
Copy link
Owner

Is this my code or did you modify it? It doesn't look like my original code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants