TypeError:无法读取null的属性"0" [英] TypeError: Cannot read property '0' of null

查看:268
本文介绍了TypeError:无法读取null的属性"0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用Sails.js作为框架的NodeJS项目.

I'm working on a NodeJS Project that uses Sails.js as a framework.

我要完成的是一个权限系统,其中每个组的权限由复选框设置,我在AngularJS中使用典型形式.

What I'm trying to accomplish is a permissions system where the permissions per group are set by Check Boxes, I'm using a typical form with AngularJS.

当我单击"Sumbit"按钮时,它会在浏览器的控制台上引发此错误:

When I click my "Sumbit" button it throws this error at my Browser's console:

angular.1.3.js:11594 TypeError: Cannot read property '0' of null
at $parseFunctionCall (angular.1.3.js:12333)
at callback (angular.1.3.js:22949)
at Scope.$eval (angular.1.3.js:14383)
at Scope.$apply (angular.1.3.js:14482)
at HTMLFormElement.<anonymous> (angular.1.3.js:22954)
at HTMLFormElement.eventHandler (angular.1.3.js:3011)(anonymous function) @ angular.1.3.js:11594

任何帮助,将不胜感激.

Any help with this would be greatly appreciated.

编辑忘记密码:
这是接收从

EDIT Forgot the Code:
Here is the code that receives the information POSTed from the form

createGroup: function(req, res) {
Groups.create({
  groupName: req.param('groupName'),
  canViewUsers: req.param('canViewUsers'),
  canEditUsers: req.param('canEditUsers'),
  canPromoteToStaff: req.param('canPromoteToStaff'),
  canViewNotes: req.param('canViewNotes'),
  canEditPermissions: req.param('canEditPermissions')
});

以下是用于捕获信息并将其发布到create函数的代码

Here is the code for that catches the information and POSTs it to the create function

angular.module('GroupsModule').controller('GroupsController', ['$scope', '$http', 'toastr', function($scope, $http, toastr) {

$scope.createGroup = {
    loading: false
  };

$scope.createGroupForm = function(){

// Set the loading state (i.e. show loading spinner)
$scope.createGroup.loading = true;


// Submit request to Sails.
$http.post('/createGroup', {
  groupName: $scope.createGroupForm.groupName,
  canViewUsers: $scope.createGroupForm.canViewUsers,
  canEditUsers: $scope.createGroupForm.canEditUsers,
  canPromoteToStaff: $scope.createGroupForm.canPromoteToStaff,
  canViewNotes: $scope.createGroupForm.canViewNotes,
  canEditPermissions: $scope.createGroupForm.canEditPermissions
})
  .then(function onSuccess(sailsResponse){
    window.location = '/groups';
  })
  .catch(function onError(sailsResponse){

    // Handle known error type(s).
    // If using sails-disk adpater -- Handle Duplicate Key
    var groupAlreadyExists = sailsResponse.status == 409;

    if (groupAlreadyExists) {
      toastr.error('That group already exists', 'Error');
    }

  })
  .finally(function eitherWay(){
    $scope.createGroup.loading = false;
  })

有右括号,但帖子中的格式未正确设置.

There are closing brackets but they aren't getting formatted correctly in the post.

最后是表单本身的代码:

And finally here is the code for the Form itself:

<!--STYLES-->
<link rel="stylesheet" href="/styles/angular-toastr.css">
<link rel="stylesheet" href="/styles/bootstrap.3.1.1.css">
<link rel="stylesheet" href="/styles/importer.css">
<link rel="stylesheet" href="/styles/style.css">
<link rel="stylesheet" href="/styles/theme.css">
<link rel="stylesheet" href="/styles/theme.min.css">
<!--STYLES END-->
<body ng-app="DashboardModule" ng-controller="DashboardController" ng-cloak>
<div class="bs-docs-section clearfix">
  <div class="row">
    <div class="bs-component">
      <nav class="navbar navbar-default">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Insomnia eSports</a>
          </div>

          <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
              <li><a href="/groups"><i class="fa fa-users" aria-hidden="true"></i> Group Management </a></li>
            </ul>

            <!--
            <form class="navbar-form navbar-left" role="search">
              <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
              </div>
              <button type="submit" class="btn btn-default">Submit</button>
            </form>
            -->
            <ul class="nav navbar-nav navbar-right">
              <li><a href="/logout">Sign Out</a></li>
            </ul>
          </div>
        </div>
      </nav>
    </div>
  </div>
</div>

<form ng-submit="createGroupForm()" id="create-group-form" class="form-signin" name="createGroupForm">
  <h2 class="form-signin-heading">Create an account</h2>
  <div class="row">

    <!-- Group Name -->
      <label>Group Name</label>
      <input type="text" class="form-control" placeholder="Group Name" name="groupName" ng-model="createGroupForm.name" ng-maxlength="25" required>

    </div>

    <!-- Can View Users -->
      <label>View Users?</label>
      <input type="checkbox" name="canViewUsers" ng-model="canViewUsers.value">

    <!-- Can View Users -->
      <label>Edit Users?</label>
      <input type="checkbox" name="canEditUsers" ng-model="canEditUsers.value">

    <!-- Can Promote To Staff -->
      <label>Promote to Staff?</label>
      <input type="checkbox" name="canPromoteToStaff" ng-model="canPromoteToStaff.value">


    <!-- Can Promote To Staff -->
      <label>Can view notes?</label>
      <input type="checkbox" name="canViewNotes" ng-model="canViewNotes.value">

    <!-- Can Promote To Staff -->
      <label>Can edit permissions?</label>
      <input type="checkbox" name="canEditPermissions" ng-model="canEditPermissions.value">

  <br/>

  <!-- Disable signup button until the form has no errors -->
  <button class="btn btn-success btn-lg btn-block" type="submit" ng-disabled="createGroupForm.$invalid">
    <span ng-show="!createGroupForm.loading">Create Group</span>
    <span class="overlord-loading-spinner fa fa-spinner" ng-show="createGroupForm.loading" ></span>
    <span ng-show="createGroupForm.loading">Preparing your new group...</span>
  </button>

  <input type="hidden" name="_csrf" value="<%= _csrf %>" />
</form>
<!--SCRIPTS-->
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/dependencies/angular.1.3.js"></script>
<script src="/js/dependencies/Base64.js"></script>
<script src="/js/dependencies/angular-toastr.js"></script>
<script src="/js/dependencies/compareTo.module.js"></script>
<script src="/js/public/signup/SignupModule.js"></script>
<script src="/js/public/groups/GroupsModule.js"></script>
<script src="/js/private/dashboard/DashboardModule.js"></script>
<script src="/js/public/homepage/HomepageModule.js"></script>
<script src="/js/private/dashboard/DashboardController.js"></script>
<script src="/js/public/groups/GroupsController.js"></script>
<script src="/js/public/homepage/HomepageController.js"></script>
<script src="/js/public/signup/SignupController.js"></script>
<!--SCRIPTS END-->
</body>

推荐答案

这是我不正确地在createGroupForm上使用$ scope而不是在createGroup上解决的.更正后的代码位如下:

This was solved by me incorrectly using $scope on createGroupForm instead of just createGroup. The corrected code bit is below:

代替:

$scope.createGroupForm.canViewUsers

使用:

$scope.createGroup.canViewUsers

这篇关于TypeError:无法读取null的属性"0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆