'书'已定义但从未使用过MEAN堆栈控制器

时间:2014-12-03 09:20:03

标签: javascript node.js mongodb controllers mean-stack

我在创建变量并在MEAN包中使用它时遇到了问题。我是基于"文章"以包为例。我在客户端控制器中看到的所有内容都是相同的,但我不确定为什么当我尝试在"书籍和#34上启动我的应用程序(带有咕噜声)时我会抓住错误;包装但不是"文章"包。

我还没有实现文章所有的控制器,这可能是个问题?

当我使用grunt启动应用时,我收到了以下错误:' book'已定义但从未使用过MEAN堆栈控制器

我相信错误在控制器中,但如果您需要查看其他文件,请告诉我。

books.js

//client-side controller

'use strict';

angular.module('mean.books').controller('BooksController', ['$scope', 'Global', 'Books',
  function($scope, Global, Books) {
    $scope.global = Global;
    $scope.package = {
      name: 'books'
    };

    $scope.hasAuthorization = function(book) {
      if (!book || !book.user) return false;
      return $scope.global.isAdmin || book.user._id === $scope.global.user._id;
    };

    $scope.create = function(isValid) {
      if (isValid) {
        var book = new Books({
          title: this.title,
          author: this.author,
          description: this.description,
          seller: this.seller
        });
      /* Not sure if we need this location thing 
        book.$save(function(response) {
          $location.path('books/' + response._id);
        });
      */

        this.title = '';
        this.content = '';
        this.description = '';
        this.seller = ''; // or this.user implement
      } else {
        $scope.submitted = true;
      }
    };

  }
]);

articles.js //这是我以

为基础的示例
'use strict';

angular.module('mean.articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Global', 'Articles',
  function($scope, $stateParams, $location, Global, Articles) {
    $scope.global = Global;

    $scope.hasAuthorization = function(article) {
      if (!article || !article.user) return false;
      return $scope.global.isAdmin || article.user._id === $scope.global.user._id;
    };

    $scope.create = function(isValid) {
      if (isValid) {
        var article = new Articles({
          title: this.title,
          content: this.content
        });
        article.$save(function(response) {
          $location.path('articles/' + response._id);
        });

        this.title = '';
        this.content = '';
      } else {
        $scope.submitted = true;
      }
    };

    $scope.remove = function(article) {
      if (article) {
        article.$remove(function(response) {
          for (var i in $scope.articles) {
            if ($scope.articles[i] === article) {
              $scope.articles.splice(i, 1);
            }
          }
          $location.path('articles');
        });
      } else {
        $scope.article.$remove(function(response) {
          $location.path('articles');
        });
      }
    };

    $scope.update = function(isValid) {
      if (isValid) {
        var article = $scope.article;
        if (!article.updated) {
          article.updated = [];
        }
        article.updated.push(new Date().getTime());

        article.$update(function() {
          $location.path('articles/' + article._id);
        });
      } else {
        $scope.submitted = true;
      }
    };

    $scope.find = function() {
      Articles.query(function(articles) {
        $scope.articles = articles;
      });
    };

    $scope.findOne = function() {
      Articles.get({
        articleId: $stateParams.articleId
      }, function(article) {
        $scope.article = article;
      });
    };
  }
]);

1 个答案:

答案 0 :(得分:1)

$scope.create函数中,您定义了book

var book = new Books({

从不使用它。这是你得到警告的原因。如果要在开发中跳过jshint警告,请使用grunt -f或在grunt配置中允许未使用的变量(如果使用,则允许使用.jshintrc)