无法读取未定义的属性“电子邮件”

时间:2018-11-10 12:52:01

标签: angularjs codeigniter-3

我正在AngularJS和CodeIgniter中构建应用程序-我在执行某些代码时遇到问题:

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' ) );

    });

下面是我的PHP登录功能:

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);
    }

他在控制台中收到的消息是:

TypeError:无法读取未定义的属性“电子邮件”     在ChildScope。$ scope.formSubmit(controllers-admin.js:549)

第549行:email : user.email,

1 个答案:

答案 0 :(得分:1)

Cannot read property 'email' of undefined-也许从ng-model user

传递密码和电子邮件是错误的

尝试这样的事情:

<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)
    }

在我的代码中,我可以轻松地console电子邮件和用户密码。

这里很矮:http://plnkr.co/edit/AeUARBrQRGHHYvVM6pd2?p=preview