0

I have a simple query that I am struggling with:

.factory('Config', function($http, DB) {
  var self = this;
  self.setValue = function(key,value) {
    console.log('setValue(value)', value);
    return DB.query("UPDATE config SET value = '"+value+"' WHERE key = '"+key+"'")
    .then(function(result){
              return DB.fetchAll(result);
          });
  }

  self.getValue = function(key) {
    return DB.query("SELECT value FROM config WHERE key = '"+key+"'")
    .then(function(result){
              return DB.fetchOne(result);
          });
  };
  return self;
})

with the following code in controller.js under the heading:

.factory('DB', function($q, DB_CONFIG) {
        var self = this;
        self.db = null;

(I took the init part of the function away for the sake of simplicity. Also DB is working well when inserting, getting and updating data.)

self.fetchOne = function(result) {
  var output = null;
  output = angular.copy(result.rows.item(0));
  return output;
};

self.query = function (sql, bindings) {
    bindings = typeof bindings !== 'undefined' ? bindings : [];
    var deferred = $q.defer();

    self.db.transaction(function (transaction) {
        transaction.executeSql(sql, bindings, function (transaction, result) {
            deferred.resolve(result);
        }, function (transaction, error) {
            deferred.reject(error);
        });
    });

    return deferred.promise;
};
self.fetchAll = function (result) {
    var output = [];

    for (var i = 0; i < result.rows.length; i++) {
        output.push(result.rows.item(i));
    }

    return output;
};

Called like so:

$scope.config.recordar = Config.getValue("recordar");

Doing a console.log returns:

Chrome Console

I am struggling to access the value: "true" which is highlighted in BLUE in the above image.

Any leads?

2 Answers 2

1
$scope.config.recordar = Config.getValue("recordar");

Does not work (ala JS). It has to be called like so:

Config.getValue("recordar").then(function(data) {
$scope.config.recordar
});
Sign up to request clarification or add additional context in comments.

Comments

0

I assume that you shall change your function declaration from :

  self.setValue = function(key,value) {
    console.log('setValue(value)', value);
    return DB.query("UPDATE config SET value = '"+value+"' WHERE key = '"+key+"'")
    .then(function(result){
              return DB.fetchAll(result);
          });
  }

to

  self.setValue = function(key,value) {
    console.log('setValue(value)', value);
    DB.query("UPDATE config SET value = '"+value+"' WHERE key = '"+key+"'")
    .then(function(result){
              return DB.fetchAll(result);
          });
  }

You will return the result of your promise, not your promise I have changed "return DB..." to "DB..."

8 Comments

Now Config.getValue("recordar") is undefined
can you please add the code of your controller that is calling the config.getValue
$scope.config.recordar = Config.getValue("recordar"); CONTROLLER HEADING: .controller('AppCtrl', function($scope, $ionicModal, $timeout, $state, Auth, Config) {
Don't know if it has an impact, but I personnaly initialise my factories with var self = {}; instead of var self = this. Can you please also add a "console.log("FACTORY START");" just after var selft ... This is to test that your factory is well injected.
Can you paste your full error message ? it looks strange that it is undefined.
|

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.