0

I am trying to return the array I got from the database using websql but its empty.

angular.module('starter.services', [])
    .factory('Products', function() {
      var db = openDatabase('db', '1.0', 'DB',5 * 1024 * 1024);
      return {
        all: function(subId) {
           var products = [];
           db.transaction(function (tx) {
           tx.executeSql('SELECT * FROM products where category_id ='+subId, [], function (tx, results) {
           var len = results.rows.length, i;
           for (i = 0; i < len; i++){
              products.push(results.rows.item(i));
              console.log(products);// this will print out the products
           }
           }, null);
          });
          console.log(products);// empty
          return products;
        },

2 Answers 2

1

It's seem to be a simple async issue.

You try to access to an array not already filled, try to use the $q service provided by angular.

angular.module('starter.services', [])
    .factory('Products', [ '$q', function($q) {
        var db = openDatabase('db', '1.0', 'DB',5 * 1024 * 1024);
        return {
            all: function(subId) {
                var defer = $q.defer();

                var products = [];
                db.transaction(function (tx) {
                    tx.executeSql('SELECT * FROM products where category_id ='+subId, [], function (tx, results) {
                        var len = results.rows.length, i;
                        for (i = 0; i < len; i++){
                            products.push(results.rows.item(i));
                        }
                        //filled
                        $q.resolve(products);

                    }, null);
                });
                return defer.promise;
            },
            ...

And if you want to use the products array

Product.all(/**subId**/).then(function(products) {
//some actions
})
Sign up to request clarification or add additional context in comments.

Comments

0

This is new for me to write the query inside client side layer. Instead of this i suggest you to either create the webservices and call the link through creating the service layer in JS which will hold only server side action URL or using the Server side framework to return the product list. You can create the service/provide to do with the following code:

module.service('productlistfetchservice', function() {
    this.productList = function($http, $scope) {
        $http({
            method : 'POST',
            url : '/productlist.action'
        }).success(
                function(data, status, headers, config) {
                    $scope.gridproductlist = data;      
                }).error(function(data, status, headers, config) {
                    alert("Error");
                });
    };
});

$scope object set the product list array for the html page under angularjs code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.