我是新來的node.js和mongoose,如果有人可以幫我解決下面的錯誤,我會很感激。TypeError:[function name]不是函數,在mongoose和node.js中
我讓通過以下功能(該功能的點PUT請求是「給予好評」的論壇帖子
o.upvote = function(post) {
return $http.put('/posts/' + post._id + '/upvote')
.success(function(data){
post.upvotes += 1;
});
};
這反過來又轉到我的路線:
指數。 JS(我的路線)
router.put('/posts/:post/upvote', function(req, res, next) {
req.post.upvote(function(err, post){
if (err) { return next(err); }
res.json(post);
});
});
而且下面是我的模型
個Posts.js
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
mongoose.model('Post', PostSchema);
PostSchema.methods.upvote = function(cb) {
this.upvotes += 1;
this.save(cb);
};
在我index.js路線,下面的錯誤被拋出就行了 「req.post.upvote」:
TypeError: req.post.upvote is not a function
什麼是'req.post'應該是什麼? –