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);
这解决了我的问题,并希望这将在未来帮助你。
- 如何压缩节点js应用程序中的文件夹和邮编下载开始后?
- 即使在为Angular js的node.js中添加标头之后,也要获得CROS错误
- Node.js从MongoDB获取请求返回undefined
- 单击上一个和下一个button,使用angularjs更改项目列表的活动状态
- 使用Express.js从Angular.js下载文件
- 混淆使用mongoose填充数据和预填充数据
- NodeJS应用程序中的AngularJS“ReferenceError:window is not defined”
- Eclipse中的Node Express Project的JavaScript文件中没有定义'angular'
- 奇怪的请求在Mean.io中重复