Ask away

NEST framework, the method not returning the value

Mohammed 01/09/17 13:13


Question CategoryConfiguration,Component,Properties,Data Binding

Number of completed projects0

Question StatusUnsolved

  • SQLite
  • NEST component
  • Database
  • JS code


I am developing the native app based on the NEST framework and angular js and jquery JS. And I m new to it. I'm customizing the SQLite components for manipulating the database. I have following code service.js angular.module('', ['imrapp.config']) .factory('DB', function(DB_CONFIG, $SQLite) { var self = this; self.db = null; self.firstRunTest = function() { if (window.nest) { self.db = SQLite.openDB({ name: }); //device } else { self.db = window.openDatabase(, '1.0', 'database', -1); // browser } return $SQLite.execute(self.db, 'SELECT name FROM sqlite_master WHERE type=(?) AND name=(?)', ['table', 'sync_table']) .then(function(result) { if (result.rows.length > 0) { console.log(result.rows.item(0).name); return result.rows.item(0).name; } else { return "No results found"; } }, function(err) { alert(err); console.error(err); }); } return self; }) controllers.js angular.module('imrapp.controllers', []) .controller('LoginCtrl', function($scope, DB) { //AuthInterceptor, $ = {}; $scope.login = function() { var firstRun = DB.firstRunTest(); console.warn("IS FIRST RUN 2 = " + JSON.stringify(firstRun)); } }); When I call DB.firstRunTest, from controller. the expected return is the value of result.rows.item(0).name but it returns the object {"$$state":{"status":0}}. The query runs perfectly fine because in method DB.firstRunTest(), console.log(result.rows.item(0).name); gives intended value. Anybody help me on this, where I m going wrong. Thanks


David 01/12/17 04:58

The problem is that you are calling an asyncronous function so the response will be done at the very end, even after the "return self;" So it will be always the same as it was when it entered the function. In order to deal with asyncronous call you must use promises. First of all the call var firstRun = DB.firstRunTest(); should be: DB.firstRunTest().then(function(returned) { firstRun = returned; }) another option is to catch the error: DB.firstRunTest().then(function(returned) { firstRun = returned; }, function(error) { //Do what you want with the error }) Then I used a more simple function for the explanation, you should define yourself as you want to: .factory('DB', function(DB_CONFIG, $SQLite, $q) { var def = $q.defer(); //Create promise $SQLite.execute(db, 'SELECT name FROM sqlite_master WHERE type=(?) AND name=(?)', ['table', 'sync_table']) .then(function(result) { def.resolve(result)/*Here you should put what you want to be in "returned" I suggest to put an object. I face problems everytime I resolve a simple var.*/ }, function(err) { alert(err); console.error(err); def.reject(err)//Here you should put what you want to return as "error" }); } return def.promise;/*The promise will be returned asyncronously so it wouldn't be return till all the asyncronous function will be resolve. If you have 2 asyncronous functions but you use the "promise.resolve()" in the first one, promise data will be catch so the second function won't return anything, so be care of "resolve" just what you need.*/ })