I have an angular service that has one function visible to the controllers using the service. ie. getData
I want my service to be able to use a helper function called modifyData in the service that manipulates the data before returning it to the service which then sends the data to the controller. I do not want to use the helper function outside of the service or have it accessible outside of the service.
app.service("dataService", function() {
/* HELPER FUNCTION */
var modifyData = function(data) {
data.modified = true;
return data;
}
this.getData = function() {
//Do a http request to get oldData variable
var newData = modifyData(oldData);
return newData;
}});
My error from Angular is Error: modifyData is undefined.
What am I doing wrong here?
EDIT: Here is my actual code since my example code should work then I might have simplified it too much.
rabApp.service("reviewService",
["$http", "$q", "beerService", "userService",
function($http, $q, beerService, userService) {
/* HELPER FUNCTIONS */
/* Builds a complete review object from a user object, beer object, and partial review object */
var buildReviewObj = function(reviewObj, switchClass) {
var deferred = $q.defer();
if(switchClass) {
reviewObj.dirClass = "left";
} else {
reviewObj.dirClass = "right";
}
//Make sure the review picture is valid and if not use default one
if(reviewObj.image.length <= 0) {
reviewObj.image = "images/default-beer-pic.jpg";
}
//Make sure the review style is present if not put a default in
if(reviewObj.style.length <= 0) {
reviewObj.style = "Unknown";
}
//Add user data
userService.getUser(reviewObj.author_id).success(function(data, status) {
//Check if we have a matched user
if(data.status === "success") {
var userObj = data.data;
//Make sure the profile picture is valid and if not use default one
if(userObj.profile_pic.length <= 0) {
userObj.profile_pic = "images/default-profile-pic.jpg";
}
reviewObj.author = userObj;
} else {
deferred.reject("Bad User object returned");
}
});
//Add beer data
beerService.getBeer(reviewObj.beer_id).success(function(data, status) {
//Check if we have a matched beer
if(data.status === "success") {
var beerObj = data.data;
reviewObj.beer = beerObj;
} else {
deferred.reject("Bad Beer object returned");
}
});
deferred.resolve(reviewObj);
}
/* Gets an array of review objects from the backend
* @return Array of Review objects
*/
this.getReviews = function() {
var deferred = $q.defer();
$http({
method: "GET",
url: "/includes/services/reviews.php",
params: { a : "getReviews",
limit : "10" }
}).success(function(data, status) {
//switchClass variable alternates the review to display left or right
var switchClass = true;
//Notify review controller that we are loading reviews
deferred.notify("loading");
//Check if we have reviews
if(data.status === "success") {
var reviews = [];
//Add beer and user data to each review
data.data.forEach(function(reviewObj, index) {
buildReviewObj(reviewObj, switchClass).then(function(data) {
switchClass = !switchClass;
reviews.push(reviewObj);
});
});
//Notify review controller that we are done
deferred.notify("finished");
//Review array built successfully return reviews
deferred.resolve(reviews);
} else {
//Couldn't get reviews return false
deferred.reject("Couldn't access reviews.php back end service");
}
}).error(function() {
//Couldn't get reviews return false
deferred.reject("Couldn't access reviews.php back end service");
});
return deferred.promise;
}}]);
I found my error I was missing
return deferred.promise;
In the helper function.... facepalm this took me 10 minutes to solve after posting here but only after I struggled for an hour an half before posting here.