Restangular error – "then is not a function" when trying to resolve promise

Issue

This is my service.

(function() {
  'use strict';

  angular
    .module('amazonScraperWebClient')
    .factory('dataService', dataService);

  /** @ngInject */
  function dataService(Restangular) {

    Restangular.setBaseUrl('http://localhost:3000');

    var data = {
      getProductList:getProductList
    };


    function getProductList() {
      return Restangular.all('products').getList();
    }

    return data;
  }
})();

This is the function in the controller:

function getProductList(){
      dataService.getProductList.then(function(products){
        console.log(products);
      },function(err){
        console.log(err);
      });
}

This is the error I see in the chrome dev tools:
dataService.getProductList.then is not a function

Can someone tell me what I may be doing wrong here? I am simply trying to perform an http GET at http://localhost:3000/products against a nodejs / express backend running on different port. Enabling CORS on the server didnt seem to make a difference. Backend call to the rest service appears to work via Postman.

Solution

Your controller should call the service function dataService.getProductList():

function getProductList(){
  dataService.getProductList().then(function(products){
    console.log(products);
  },function(err){
    console.log(err);
  });
 }

Answered By – Miyuru Ratnayake

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.