I am really new to Angular and am working on bypassing a DB call that I don't want to use any more. Between the response from the code that calls the DB and my new code that accesses a stored variable (via getter), the only difference is that the response object's $promise
value is undefined
(in the new code) instead of Object
(in the old code). This difference seems to be resulting in the following error: TypeError: undefined is not a function
; I am using AngularJS v1.2.0-rc.3 Do I have to be using an async call for the $promise
value to be set to an Object
or is there a way to manually set/cast it?
Here are some code snippets:
Factory (QueryService):
var recentResponse = null;
var getRecentResponse = function() {
return recentResponse;
};
var doQuery = function (input, handler, errorHandler) {
MyResource.saveInput(input, function (response) {
// save JSON response locally (NEW!)
recentResponse = response;
// save JSON response to DB via handler (old)
handler(response);
},
function (errors) {
errorHandler(errors);
});
};
var getQueryById = function(id, handler) {
// query the DB
MyResource.getObjectWithId(id, function(response) {
handler(response);
});
};
return {
doQuery: doQuery,
getQueryById: getQueryById,
getRecentResponse: getRecentResponse
};
Controller:
function loadQueryById(id) {
// call the DB query
QueryService.getQueryById(id, function(response) {
$scope.query = response;
$scope.query.val2 = $scope.getVal2();
});
}
function loadLastQueryResponse() {
// load from local var
$scope.query = QueryService.getRecentResponse();
$scope.query.val2 = $scope.getVal2();
}
loadQueryById($routeParams.id);
loadLastQueryResponse();
$scope.getVal2 = function() {
return $scope.query.value2 ? JSON.parse($scope.query.value2) : "";
};
When loadQueryById()
is called $scope.query
is set with the $promise
field = Object
; when loadLastQueryResponse()
is called, all the $scope.query
fields are exactly the same except $promise
is undefined
, and the following line fails with the error when trying to call $scope.getVal2()
:
TypeError: undefined is not a function
at loadLastQueryResponse (MyController.js:20)
The $scope.query.$resolved
field is true
in both cases.
Does my Angular version have something to do with it, or the fact that I am not waiting on a promise to be fulfilled (because I don't need to be)? I also tried calling QueryService.getRecentResponse()
with a handler, and I also tried using the then()
format (which I may or may not have implemented correctly) but the result was the same.
Function and variable names have been changed to protect the innocent. Thanks in advance!
As answered by @laurent in the comments, the issue was that once the request was no longer calling the DB asynchronously, and instead calling the function synchronously; so the $scope.getVal2 = function () {..}
declaration had to be moved above function loadLastQueryResponse(){..}
so the calling code within the latter knows about the function implementation of the former.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.