【问题标题】:'book' is defined but never used MEAN stack controller'book' 已定义但从未使用过 MEAN 堆栈控制器
【发布时间】:2014-12-03 09:20:02
【问题描述】:

我在创建变量并在 MEAN 包中使用它时遇到问题。我将它基于作为示例的“文章”包。我在客户端控制器中看到的所有内容都是相同的,但我不确定为什么当我尝试在“books”包而不是“articles”包上启动我的应用程序(使用 grunt)时会遇到错误.

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

当我用 grunt 启动应用程序时,我得到这个错误:'book' is defined but never used MEAN stack controller

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

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

【问题讨论】:

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


    【解决方案1】:

    $scope.create 函数中定义了book

    var book = new Books({
    

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

    【讨论】:

    • 在您的代码中,您注释掉了实际使用您创建的变量的唯一行。
    猜你喜欢
    • 2023-04-04
    • 2014-07-08
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 2020-03-17
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多