I'm new to node.js and mongoose, and I'd appreciate it if someone could help me with the below error.
I make a put request via the following function (the point of the function is to "upvote" a forum post.
o.upvote = function(post) {
return $http.put('/posts/' + post._id + '/upvote')
.success(function(data){
post.upvotes += 1;
});
};
That in turn goes to my route:
index.js (my route)
router.put('/posts/:post/upvote', function(req, res, next) {
req.post.upvote(function(err, post){
if (err) { return next(err); }
res.json(post);
});
});
And below is my model
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);
};
In my index.js route, the below error is thrown on the line "req.post.upvote":
TypeError: req.post.upvote is not a function
req.postsupposed to be?