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

我想问你在这个代码的帮助。 编辑和删除工作正常,但我不能添加文章到数据库(mongodb)。

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

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

文章不是一个function

TypeError:文章不是articles.js中的函数

我不知道对象(Article)有什么问题。 当然是例子:AngularJS中的项目 – 通过构build10个项目来学习 – 第7部分:知识库。 请帮我解决这个代码。 我想了解为什么它不起作用。

表单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(条)

before var Article = require('../ models / article');

  undefined 

和var Article = require('../ models / article')之后

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

好朋友 – 作品

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

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

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

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