Ikram Bk
Ikram Bk

Reputation: 177

Ionic service is not a function

this current code returns this error :TypeError: compteService.InsertCompte is not a function because of InsertCompte

service.js

.factory('compteService', function($http, GetComptes) {


        var getComptes = function() {
            return $http.get(GetComptes.url);
        };

        return {
            getComptes: getComptes
        };
        var InsertCompte=function(user,compte){
            var strFinal = "[" + JSON.stringify(user) + "," +
                       JSON.stringify(account) + "]";
            return $http.post("http://localhost:26309/api/Compte/addUserV", strFinal)
            .then(function () {
                 $log.info("Insert Successful");
                return;
            });
        };
});

controller.js

.controller('AjouterCompteCtrl', function($scope,compteService) {
 $scope.InsertAccount = function (user,account) {
            compteService.InsertCompte(user,account)
            .success(function() {
            console.log('success');

            }).error(function() {               
                console.log('error');
            });

        };    
  });

Upvotes: 0

Views: 1129

Answers (2)

Aditya Singh
Aditya Singh

Reputation: 16650

This is happening because of 2 issues in your code:

  1. You are not returning method InsertCompte as part of the returned object from service

    return {
        getComptes: getComptes,
        InsertCompte: InsertCompte
    };
    
  2. You are using a function expression as opposed to a function declaration to define the method InsertCompte. So even if you try returning the method InsertCompte as part of the service object mentioned in #1, your code won't work. This is a classic example of abuse of revealing module pattern. In case of angular factories always use the revealing module pattern along with function declarations.

    .factory('compteService', function($http, GetComptes) {
      return {
        getComptes: getComptes,
        InsertCompte: InsertCompte
      };
    
    
      function getComptes () {
        return $http.get(GetComptes.url);
      };
    
      function InsertCompte (user,compte){
        var strFinal = "[" + JSON.stringify(user) + "," +
                   JSON.stringify(account) + "]";
        return $http.post("http://localhost:26309/api/Compte/addUserV", strFinal)
        .then(function () {
             $log.info("Insert Successful");
            return;
        });
      };
    });
    

With the definition of factory in step 2, your code will work

Upvotes: 1

Nazrul Islam
Nazrul Islam

Reputation: 348

you can try rewriting your factory code following way:

angular.module("compteService",[])
.factory('compteService', function($http) {
var api_url ="http://localhost:26309/api/Compte/";
    return{
        getComptes:function(){
            return $http.get(api_url);
        },
        InsertCompte:function(user,compte){
              var strFinal = "[" + JSON.stringify(user) + "," +
                   JSON.stringify(account) + "]";
        return $http.post(api_url+"addUserV", strFinal)
        .then(function () {
             $log.info("Insert Successful");
            return;
        });
        }
    };
});

Upvotes: 0

Related Questions