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 trialraul cortes
5,745 PointsProblem posting in question/:qID/answers
In the "Build a REST API with Express" course at the final stage, I am trying to test with postman the Post method when I want to post an answer, the thing is that the body parser shows me an error saying that it "cant read the property push of an undenifed ". So I guess that the undefined thing is not working is the answers property in req.question.answers before the push() because I dont think the problem is the req.body .... Thanks for your time. Kind of new about this.
Here is the code in the routes file
'use strict';
var express= require("express");
var router= express.Router();
var Questions= require("./models/models").Question;
//preloaders para los params de req
router.param("qID",function(req,res,next,id){
Questions.find(id,function(err,doc){
if(err)return next(err);
if(!doc){
err= new Error("Not Found");
err.status=404;
return next(err);
}
req.question=doc;
return next();
});
});
router.param("aID",function(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);
}
next();
});
//GET /question para visualizar todas
router.get("/", function(req,res,next){
Questions.find({})
.sort({createdAt:-1})
.exec(function(err,question){
if(err) return next(err);
res.json(question);
});
});
//POST /question para crear
router.post("/",function(req,res,next){
var question = new Questions(req.body);
question.save(function(err,question){
if(err) return next(err);
res.status(200);
res.json(question);
});
});
//GET /question/:id para visualizar por Id
router.get("/:qID", function(req,res,next){
res.json(req.question);
});
//POST/question/:id/answers
router.post("/:qID/answers",function(req,res,next){
req.question.answers.push(req.body);
req.question.save(function(err,question){
if(err) {
console.error(err);
return next(err);
}
res.status(201);
res.json(question);
});
});
//PUT /question/:id/answers/:id
//edit specific answer
router.put("/:qID/answers/:aID",function(req,res,next){
req.answer.update(req.body,function(err,result){
if(err) return next(err);
res.json(result);
});
});
//DELETE /question/:id/answers/:id
//delete specific answer
router.delete("/:qID/answers/:aID",function(req,res,next){
req.answer.remove(function(err){
req.question.save(function(err,question){
if(err) return next(err);
res.json(question);
});
});
});
//POST /question/:id/answers/:id/upvote
//POST /question/:id/answers/:id/downvote
//vote specific anser
router.post("/:qID/answers/:aID/:dir-vote",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;
1 Answer
raul cortes
5,745 PointsI can get and post questions, answers are not working....I cant test further because it needs the answer post (I could do it manually, but is not the idea)
miikis
44,957 PointsCan you post the rest of the code?
raul cortes
5,745 PointsI found the problem...it was a typing error. In the param method for 'qID' , I used find instead of findById, so the push could'nt run because I didn't have the question Id.
Thx a lot anyway, I always appreciate when people spend their time for helping others.
Hugs from Chile :)
miikis
44,957 Pointsmiikis
44,957 PointsDo the other endpoints work okay?