0

After clicking on register button the site is showing success is not a function error. its not getting registered because of it please help!

This is the error

    angular.js:14525 TypeError: $http.post(...).success is not a function
    at b.$scope.register (chirpapp.js:63)
    at fn (eval at compile (angular.js:15358), <anonymous>:4:144)
    at e (angular.js:26994)
    at b.$eval (angular.js:18161)
    at b.$apply (angular.js:18261)
    at HTMLFormElement.<anonymous> (angular.js:26999)
    at HTMLFormElement.dispatch (jquery.min.js:3)
    at HTMLFormElement.q.handle (jquery.min.js:3)
(anonymous) @ angular.js:14525
(anonymous) @ angular.js:11008
$apply @ angular.js:18266
(anonymous) @ angular.js:26999
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3

This is my app.js

var app = angular.module('chirpApp', ['ngRoute']).run(function($rootScope){
     $rootScope.authenticated = false;
  $rootScope.current_user = ''; 
});


app.config(function($routeProvider){
  $routeProvider
    //the timeline display
    .when('/', {
      templateUrl: 'main.html',
      controller: 'mainController'
    })
    //the login display
    .when('/login', {
      templateUrl: 'login.html',
      controller: 'authController'
    })
    //the signup display
    .when('/register', {
      templateUrl: 'register.html',
      controller: 'authController'
    });
});

app.controller('mainController', function ($scope) {
    $scope.posts = [];
    $scope.newPost = {
        created_by: '',
        text: '',
        created_at: ''
    };

    $scope.post = function () {
        $scope.newPost.created_at = Date.now();
        $scope.posts.push($scope.newPost);
        $scope.newPost = {
            created_by: '',
            text: '',
            created_at: ''
        };
    };
});

app.controller('authController', function($scope, $http, $rootScope, $location){
  $scope.user = {username: '', password: ''};
  $scope.error_message = '';

  $scope.login = function(){
    $http.post('/auth/login', $scope.user).success(function(data){
      if(data.state == 'success'){
        $rootScope.authenticated = true;
        $rootScope.current_user = data.user.username;
        $location.path('/');
      }
      else{
        $scope.error_message = data.message;
      }
    });
  };

  $scope.register = function(){
    $http.post('/auth/signup', $scope.user).success(function(data){
      if(data.state == 'success'){
        $rootScope.authenticated = true;
        $rootScope.current_user = data.user.username;
        $location.path('/');
      }
      else{
        $scope.error_message = data.message;
      }
    });
  };
});

After clicking on register button the site is showing success is not a function error. its not getting registered because of it please help! ckech "authcontroller" the error is in register so it might be in $scope.register=function(...)

Juhi Shaw
  • 1
  • 1
  • 1
  • It sounds like it's probably something to do with this: [https://stackoverflow.com/questions/33531336/angularjs-error-success-is-not-a-function](https://stackoverflow.com/questions/33531336/angularjs-error-success-is-not-a-function) – Chris Mack Oct 07 '17 at 18:03
  • 1
    `.success` & `.error` callbacks are deprecated, check [here](https://code.angularjs.org/snapshot/docs/guide/migration#migrate1.5to1.6-ng-services-$http), Instead use `$http.post('url').then(function(){})` – Pankaj Parkar Oct 07 '17 at 18:03
  • i replaced success with .then console is not showing any error but registeraation is still not working – Juhi Shaw Oct 07 '17 at 18:31
  • It is not a matter of just substituting `.then` for `.success`. The `.then` method returns a response object instead of properties spread over several arguments. For more information, read the referenced answers. Or read [AngularJS Developer Guide - Migrating to V1.6 - $http](https://code.angularjs.org/snapshot/docs/guide/migration#migrate1.5to1.6-ng-services-%24http). – georgeawg Oct 07 '17 at 18:44
  • then(function onSuccess(response) { // Handle success if(response.state == 'success'){ $rootScope.authenticated = true; $rootScope.current_user = response.user.username; $location.path('/'); } else{ $scope.error_message = response.message; } – Juhi Shaw Oct 07 '17 at 18:44
  • i did this but still not working – Juhi Shaw Oct 07 '17 at 18:44
  • used statusText insteadof "state" not working – Juhi Shaw Oct 07 '17 at 18:55

0 Answers0