Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Eric Inoti
819 Pointsreq.answer.vote is not a function"
My code is alright, and all my dependencies installed. however when I call the vote-up POST i get the error above. The code in my route.js file is as follows:
'use strict' const express = require('express'); const router = express.Router(); const Question = require('./models'); router.param("qID", (req, res, next, id) => { Question.findById(id, (err, doc) => { if (err) next(err); if (!doc) { err = new Error("Not found"); err.status = 404; return next(err); } req.question = doc; return next(); }); });
router.param("aID", (req, res, next, id) => { req.answer = req.question.answers.id(id);
if (!req.answer) {
err = new Error("Not found");
err.status = 404;
return next(err);
}
return next();
}); //GET /questions //route for getting questions router.get('/', (req, res) => { // Return all the questions Question.find({}) .sort({ createdAt: -1 }) .exec((err, questions) => { if (err) return next(err); res.json(questions);
});
}); //POST /questions //route for creating questions router.post('/', (req, res) => { let question = new Question(req.body); question.save((err, question) => { if (err) return next(err); res.status(201); res.json(question); }); // Return all the questions
}); //GET /questions/:id //route for getting questions router.get('/:qID', (req, res) => { // Return all the questions res.json(req.question);
}); //POST /questions/:id/answers //route for creating questions router.post('/:qID/answers', (req, res) => { // Return all the req.question.answers.push(req.body); req.question.save((err, question) => { if (err) return next(err); res.status(201); res.json(question); }); });
//PUT /questions/:qid/answers/:id //Edit a specific answer router.put('/:qID/answers/:aID', (req, res) => { // Return all the questions req.answer.update(req.body, (err, result) => { if (err) return next(err); res.json(result); });
});
//DELETe /questions/:qid/answers/:id //Delete a specific answer router.delete('/:qID/answers/:aID', (req, res) => { // Return all the questions req.answer.remove((err) => { req.question.save((err, question) => { if (err) return next(err); res.json(question); });
});
});
// POST/questions/:qID/answers/:aID/vote-up // POST/questions/:qID/answers/:aID/vodte-down // Vote on a specific answer router.post("/:qID/answers/:aID/vote-:dir", function(req, res, next) { if (req.params.dir.search(/^(up|down)$/) === -1) { var err = new Error("Not Found"); err.status = 404; next(err); } else { req.vote = req.params.dir; next(); } }, function(req, res, next) { req.answer.vote(req.vote, function(err, question) { if (err) return next(err); res.json(question); }); });
module.exports = router;
And my models.js file is as follows
'use strict' const mongoose = require('mongoose'); const Schema = mongoose.Schema; let sortAnswers = function(a, b) { // -negativ a before b // 0 no change //+ postive a after b if (b.votes == a.votes) { return b.updatedAt - a.updatedAt; } return b.votes - a.votes; } const AnswerSchema = new Schema({ text: String, createdAt: { type: Date, default: Date.now }, updatedAt: { type: Date, default: Date.now }, votes: { type: Number, default: 0 } }); const QuestionSchema = new Schema({ text: String, createdAt: { type: Date, default: Date.now }, answers: [AnswerSchema] }); AnswerSchema.method("update", function(updates, callback) { Object.assign(this, updates, { updatedAt: new Date() }); this.parent().save(callback) });
AnswerSchema.method("vote", function(vote, callback) { if (vote === "up") { this.votes += 1; } else { this.votes -= 1; } this.parent().save(callback); });
QuestionSchema.pre("save", function(next) { this.answers.sort(sortAnswers); next(); });
module.exports = mongoose.model('Question', QuestionSchema)
2 Answers
Eric Inoti
819 PointsHi Andrew. Found out my error! I assigned the Answer Schema to the parent object and created the vote method afterwards, so there was no reference to it. Thanks! I did this
const AnswerSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
votes: { type: Number, default: 0 }
});
const QuestionSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
answers: [AnswerSchema]
});
AnswerSchema.method("update", function(updates, callback) {
Object.assign(this, updates, { updatedAt: new Date() });
this.parent().save(callback)
});
AnswerSchema.method("vote", function(vote, callback) {
if (vote === "up") {
this.votes += 1;
} else {
this.votes -= 1;
}
this.parent().save(callback);
});
In stead of this :
const AnswerSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
votes: { type: Number, default: 0 }
});
AnswerSchema.method("update", function(updates, callback) {
Object.assign(this, updates, { updatedAt: new Date() });
this.parent().save(callback)
});
AnswerSchema.method("vote", function(vote, callback) {
if (vote === "up") {
this.votes += 1;
} else {
this.votes -= 1;
}
this.parent().save(callback);
});
const QuestionSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
answers: [AnswerSchema]
});
Andrew Young
Courses Plus Student 639 PointsThat's good if you find your way out I just see your comment of snapshot and you find your way out! As you find your way out you'll learn more than you ask someone to give you a guide or answer.
So since you find the way it'll be good if you mark your solution to the best answer so when other student have same issue they know how to solve
Best wishes
Eric Inoti
819 PointsHi Andrew,
Here's a snapshot https://w.trhou.se/f8qjsjss6f
Andrew Young
Courses Plus Student 639 PointsAndrew Young
Courses Plus Student 639 PointsCan you open a workspace with the code you have issue and take a snapshot?