I'm trying to do a http request inside Angular.js factory. Initially I had used Fetch API and the console didn't show any error. I implemented it using $http
along with $q
and now it shows errors on my console even though the functionality seems to work.
I'm getting
"Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.0/$rootScope/inprog?p0=%24digest
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:68:12
at beginPhase (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:16073:15)
at Scope.$apply (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:15817:11)
at n.<anonymous> (https://cdpn.io/cp/internal/boomboom/pen.js?key=pen.js-d35c4e85-c842-3510-cf4e-9d0946fca47f:96:16)
at n.emit (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:1:2344)
at https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:4:24433
at n (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:1:8521)
at n (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:2:26916)
at n._dispatchAlgoliaResponse (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:4:24330)
at processQueue (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:14454:28)"
I'm new to Angular.js and not sure what's going on.
The codepen of my implementation https://codepen.io/cmgchess/pen/GRMLLwV
my factory
var index = 'bestbuy';
var alSH = angular.module('AlgoliaSearchHelper', ['ngSanitize']);
// Expose the helper
alSH.factory('helper',helper);
helper.$inject = ['$http', '$rootScope', '$q'];
function helper($http, $rootScope, $q) {
var customSearchClient = {
search(requests) {
var deferred = $q.defer();
$http.post('https://algolia-backend-search.herokuapp.com/search',{requests}).success(function (response) {
deferred.resolve(response);
});
return deferred.promise;
}
// search(requests, cb) {
// return fetch('https://algolia-backend-search.herokuapp.com/search', {
// method: 'post',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({ requests }),
// }).then(function(res){return res.json()}).then(cb)
// }
};
return algoliasearchHelper(customSearchClient, index, {
disjunctiveFacets: ['category'],
hitsPerPage: 7,
maxValuesPerFacet: 3
});
};