cfjedimaster / Cordova-Examples

A collection of Cordova/Ionic/etc demos.
982 stars 1.07k forks source link

Promise don't return anythings #36

Open ChandanShankar opened 6 years ago

ChandanShankar commented 6 years ago

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 commented 6 years ago

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