简体   繁体   中英

'book' is defined but never used MEAN stack controller

I'm having problems creating a variable and using it in a MEAN package. I'm basing it off of the "articles" package that comes as an example. Everything I see is the same in the client-side controller, but I'm not sure why I'm catching the error when I try to start my app (with grunt) on the "books" package but not the "articles" package.

I have not implemented all the controllers that articles has yet, that may be an issue?

When I start the app with grunt, I get this error on : 'book' is defined but never used MEAN stack controller

I believe the error is in the controller, but if you need to see other files please let me know.

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 //this is the example that I'm basing it from

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

In $scope.create function you defined book

var book = new Books({

and never use it. That's reason you get warning. If you want to skip jshint warnings in development use grunt -f or allow unused variables in your grunt configuration (or .jshintrc if you use it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM