danko12
danko12

Reputation: 79

Cannot read property 'email' of undefined

I am building an application in AngularJS and CodeIgniter - I have a problem with the execution of a certain code:

controllersAdmin.controller('login', function( $scope, $http, store){

       $scope.formSubmit = function (user){

          $http({
          method: 'POST', url: 'api/admin/users/login/' , 
          data: {
            email : user.email,
            password : user.password
          }}
          ).then(function ( data ){ 

            $scope.submit = true;
            $scope.error = data.error;

            if ( !data.error )
          {
            store.set('token', data.token)
          }


       },function (error){
          console.log('Blad we wczytywaniu danych');
       });
       };

       console.log( store.get( 'token' ) );

    });

Below is my PHP login function:

public function login()
    {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $password = crypt( $password, config_item('encryption_key'));

        $login = $this->Users_model->login($email, $password);

        if ( !$login ) {

            $output['error'] = 'Błędne hasło lub email';

        }
        else
        {
            $token = $this->jwt->encode( array(
                'userId' => $login ->id,
                'user' => $login ->user,
                'email' => $login ->email,
                'role' => $login ->role
            ), config_item('encryption_key'));
            $output['token'] = $token;
        }
        echo json_encode($output);
    }

The message he receives in the console is:

TypeError: Cannot read property 'email' of undefined at ChildScope.$scope.formSubmit (controllers-admin.js:549)

Line 549: email : user.email,

Upvotes: 0

Views: 888

Answers (1)

BartoszTermena
BartoszTermena

Reputation: 1487

Cannot read property 'email' of undefined - Maybe you are wrong with passing your password and email from ng-model user

Try something like this :

<h2 class="form-signin-heading">Please sign in</h2>
<div class="form-group">
  <input class="form-control" placeholder="Username" ng-model="user.email">
</div>
<div class="form-group">
  <input class="form-control" placeholder="Password" type="password" ng-model="user.password" >
</div>
  <button class="btn btn-lg btn-success btn-block" ng-click="formSubmit(user)">Login</button>

JS:

   $scope.formSubmit = function (user){
       console.log(user)
       console.log("email : ", user.email )
       console.log("password: ", user.password)
    }

In my code i can easly console email and user password.

Here is plunker: http://plnkr.co/edit/AeUARBrQRGHHYvVM6pd2?p=preview

Upvotes: 1

Related Questions