Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to cordova, Ionic framework and angular and have hit a road block with using a factory .

my factory is setup as follows:

    angular.module('test.factories', ['ionic'])
.factory('dataFactory', function($http, $q, $timeout){
 dataFactory.getItems = function(search) {
  var deferred = $q.defer();
 //rest of code to get stuff from SB works fine

//an array used to push items to from sqlite3 tx.executeSql 
returnData = []; 
deferred.resolve(returnData);
return deferred.promise;
}
}

all that works just fine, I get data into my controller and if its a list the data renders just fine! using ng-repeat in the view

however, I can't access a specific item in the returned array

eg

dataFactory.getItems("name").then(function(results){
   $scope.results = results; //ng-repeat works fine with this
   var a1 = results[0]; //returns undefined
   for (var i=0;i<results.length;i++) { //doesn't enter the loop}
   console.log(results); //shows [{"item","value"}] - JSON.parse returns an error
   //chrome devtool bar shows it as an array, and I can drill down into the elements! so
  $scope.result = results[0]; //nothing is set
});

this is causing some confusion, and issues as in some instances only 1 item is returned, and I'd like to render specific properties in the view eg {{result.property}}

as I said, I'm very new to all these frameworks, so may have done something totally wrong with the factory etc, but it's bugging me now lol how can an array be and array, but not ?!?

any help would be most appreciated!

cheers guys

share|improve this question
    
maybe the results are wrapped in an object. if you console.log the results inside then what do you see ? –  haki Jun 12 at 12:07
    
the usual drilling down into array view, that's what's confusing me the most! I've tried eval() and all sorts.. Array[1] 0: Object length: 1 proto: Array[0] –  BadWolf Jun 12 at 12:12
    
JSON.stringify returns empty string typeof() returns object –  BadWolf Jun 12 at 12:21
    
just print it to the console add see whats coming out. maybe it's malformed. –  haki Jun 12 at 12:27
    
var output = ""; for (var property in item) { output += property + ': ' + item[property]+'; '; } console.log(output); //empty console.log(item.toString()); //undefined console.log(item); : //Array[1] 0: Object - this expands into the json properties as expected length: 1 proto: Array[0] ahhh no formatting! tldr; - gives the object explorer view for an array –  BadWolf Jun 12 at 12:35
add comment

1 Answer

BAHHHHH rookie mistake!!!

In my factory I had placed deferred.resolve(data) in the wrong place, I've now added it below the row iteration in the tx.executesql success function and all is well!

thanks for trying Haki

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.