Michael Schmidt Michael Schmidt - 5 months ago 53
AngularJS Question

$q.when not a function?

I am trying to create a simple application that has a main list with detail views, but I can't get past this error:

ionic.bundle.js:26794 TypeError: $q.when is not a function
at Object.getAllObjects (app.services.js:13)
at app.controller.js:11
at ionic.bundle.js:56230
at Object.ready (ionic.bundle.js:2140)
at Object.ready (ionic.bundle.js:56223)
at new MainController (app.controller.js:7)
at Object.instantiate (ionic.bundle.js:18010)
at $controller (ionic.bundle.js:23412)
at self.appendViewElement (ionic.bundle.js:59900)
at Object.render (ionic.bundle.js:57893)


The code it is referring to is:

app.factory('DatabaseService', ['$q', '$http','$rootScope',

function DatabaseService( $rootScope, $q, $http) {
var _db;


var _content;

return {
initDB: initDB,
getAllObjects: function getAllObjects() {
if (!_content) {
return $q.when(_db.allDocs({ include_docs: true}))
.then(function(docs) {


_content= docs.rows.map(function(row) {

return row.doc;
});


_db.changes({ live: true, since: 'now', include_docs: true})
.on('change', onDatabaseChange);

return _content;
});
} else {

return $q.when(_content);
}
},
getObject: function getObject(id){
var index = findIndex(($rootScope._content),id);
return $rootScope._content[index];
}

};


It seems as if all the dependencies are correct, so why am I getting this strange error? It seems that most of my recent errors have been "not a function" errors but I can't figure out why.

Answer

You mixed your injection :

['$q', '$http','$rootScope',function DatabaseService( 
  $rootScope, $q, $http) {  ...}

So in fact your $q contains $http, reorder this and it will work.