Getting $cordovaSQLite.execute result from a factory function

am trying to use factory to get an sqlite select result to use it in my controller

  myDataFactory.FillDishesByCategory = function (CategoryNo) {
            $ionicPlatform.ready(function () {
                var query = "SELECT id , dishname , dishdiscription , dishprice , dishcategory , category , thumb_base64 , qty , backcolor  FROM dishesmenu WHERE dishcategory = ?";
                $cordovaSQLite.execute(db, query, [CategoryNo]).then(function (result) {
                    return result;
                });
            })
        }

and in controller am using

 var res = myDataFactory.FillDishesByCategory(CategoryNo);
    if (res.rows.length > 0) {
        for (var i = 0; i < res.rows.length; i++) {
            $scope.selecteddishesmenu.push({
                id: res.rows.item(i).id,
                dishname: res.rows.item(i).dishname,
                dishdiscription: res.rows.item(i).dishdiscription,
                dishprice: res.rows.item(i).dishprice,
                dishcategory: res.rows.item(i).dishcategory,
                category: res.rows.item(i).category,
                thumb_base64: res.rows.item(i).thumb_base64,
                qty: res.rows.item(i).qty,
                backcolor: res.rows.item(i).backcolor
            });
        }

    };

am “getting rows undefiened”

any help on this ?

since the query returns a promise, just pass the promise back to the caller…

  myDataFactory.FillDishesByCategory = function (CategoryNo, callback) {
     $ionicPlatform.ready(function () {
          var query = "SELECT id , .....   FROM dishesmenu  WHERE dishcategory = ?";
          $cordovaSQLite.execute(db, query, [CategoryNo]).then(function (result) {
                 callback(result);
           });
      })
  }

then

myDataFactory.FillDishesByCategory(CategoryNo, function(_results) {
    if (_results.rows.length > 0) {
        // do something magical!!
    }
});
1 Like

Thanks alot , its working now :blush:

it works perfect, thanks!