我想問你在這段代碼中的幫助。 編輯和刪除效果很好,但我無法將文章添加到數據庫(mongodb)。在瀏覽器控制檯TypeError:文章不是函數,POST 500(內部服務器錯誤)
*錯誤:
POST http://localhost:5000/articles 500 (Internal Server Error)
Article is not a function
TypeError: Article is not a function in articles.js
我不知道什麼是錯的對象(文章)。示例當然是:在AngularJS中的項目 - 通過構建10個項目來學習 - 第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(第)
變種條之前=要求( '../模型/物品');
undefined
和VAR文章後=需要( '../型號/條')
{ getArticles: [Function],
getArticleById: [Function],
getArticlesByCategory: [Function],
createArticle: [Function],
updateArticle: [Function],
removeArticle: [Function] }
您是否可以在var Article = require('../ models/article')後嘗試「console.log(Article)」;和「新文章」之前? – valepu
console.log(Article) var Article = require('../ models/article'); 未定義 和VAR第=要求( '../模型/物品') {getArticles後:[功能], getArticleById:[功能], getArticlesByCategory:[功能], createArticle:[功能] , 更新文章:[功能], 刪除文章:[函數]} – Kuba
您在article.js中有錯字。它說module.exPROTS。 – tomtom