TypeError:文章不是函数,POST 500(内部服务器错误)

时间:2016-05-15 09:20:36

标签: angularjs node.js mongodb express

我想在此代码中向您寻求帮助。 编辑和删除效果很好,但我无法在数据库中添加文章(mongodb)。

*浏览器控制台中的错误:

  

POST http://localhost:5000/articles 500(内部服务器错误)

     

文章不是函数

     

TypeError:文章不是articles.js

中的函数

我不知道对象(文章)有什么问题。示例当然是:AngularJS中的项目 - 通过构建10项目 - 第7节:KnowledgeBase来学习。请帮我解决这个问题。我想明白为什么它不起作用。

表格html

<div ng-controller="ArticlesCreateCtrl">
<h3>Nowy</h3>
<form ng-submit="addArticle()">
  <div class="form-group">
    <label>Title</label>
    <input type="text" class="form-control" ng-model="title" name="title">
    <label>Category</label>
    <select  class="form-control" ng-model="category" name="category">
        <option ng-repeat="category in categories" value={{category.name}}">
        {{category.name}}</option>
    </select>
    <label>Body text</label>
    <textarea class="form-control" ng-model="body" name="body"></textarea>
  </div>
    <button type="submit" class="btn btn-default">Submit</button>
    <a href="#/categories"  class="btn btn-default">Canel</a>
</form>
</div>

ArticlesCreateCtrl 控制器

.controller('ArticlesCreateCtrl', ['$scope', '$http', '$routeParams', '$location',
 function($scope, $http, $routeParams, $location) {
    $http.get('/categories').success(function(data){
        $scope.categories = data;
    });

    $scope.addArticle = function(){
        var data = {
            title: $scope.title,
            body: $scope.body,
            category: $scope.category
        };

        $http.post('/articles', data).success(function(data, status){
            console.log(status);
        });
        $location.path('/articles');
    }
}])

articles.js //错误

var express = require('express');
var router = express.Router();
var Article = require('../models/article');

router.post('/', function(req, res, next) {
     var title = req.body.title;
     var category = req.body.category;
     var body = req.body.body;

     ###error in this line ###
     var newArticle = new Article({
         title: title,
         category: category,
         body: body
     });

     Article.createArticle(newArticle, function(err, article) {
         if(err) {
         console.log(err);
         }
            res.location('/articles');
            res.redirect('/articles');
     });
});

module.exports = router;

article.js

var mongoose = require('mongoose');

var artSchema = mongoose.Schema({
    title: {
    type: String,
    index: true,
    required: true
    },
    body: {
    type: String,
    required: true
    },
    category:{
    type: String,
    index: true,
    required: true
    },
    date:{
    type:Date,
    default: Date.now
    }
});

var Article =  module.exprots =  mongoose.model('Article', artSchema);

module.exports.createArticle = function(newArticle, callback){
newArticle.save(callback);
};

console.log(文章)

之前的var Article = require('../ models / article');

      undefined 

以及var Article = require('../ models / article')

之后
    { getArticles: [Function],
      getArticleById: [Function],
      getArticlesByCategory: [Function],
      createArticle: [Function],
      updateArticle: [Function],
      removeArticle: [Function] }

1 个答案:

答案 0 :(得分:0)

好友 - 工作

我不知道这是不是最好的方式,但只是我改变了这一行:

    var Article =  module.exprots =  mongoose.model('Article', artSchema);

    var Article;
    module.exports =  Article =  mongoose.model('Article', artSchema);

这解决了我的问题,并希望这将在未来帮助你。