【发布时间】:2016-02-13 10:21:57
【问题描述】:
我是 node.js 和 mongoose 的新手,如果有人能帮助我解决以下错误,我将不胜感激。
我通过以下函数提出了一个 put 请求(该函数的目的是“upvote”一个论坛帖子。
o.upvote = function(post) {
return $http.put('/posts/' + post._id + '/upvote')
.success(function(data){
post.upvotes += 1;
});
};
这反过来又去了我的路线:
index.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 不是函数
【问题讨论】:
-
req.post应该是什么?
标签: javascript node.js mongodb mongoose