My JSON file is as follows and my router router.get('/books/:bookid', ctrlbooks.booksReadOne); returns the following JSON which is as expected but if I want to return just the title for say for api of this kind with given id router.get('/books/:bookid/titles/:titleid', ctrlbooks.titlesReadOne); it wont work am i missing something?
[
{
"_id": "58dd21c3cb77090b930b6063",
"bookAuthor": "George Orwell",
"titles": [
{
"title": "Animal Farm",
"_id": "58dd3f2701cc081056135dae",
"reviews": [
{
"author": "Lisa",
"rating": 4,
"reviewText": "this is a review",
"_id": "58dd8e13876c0f16b17cd7dc",
"createdOn": "2017-03-30T23:00:35.662Z"
}
],
"favouredBy": [
"bb, aa, cc"
]
},
{
"title": "1984",
"_id": "58dd42a59f12f110d1756f08",
"reviews": [
{
"author": "jessy",
"rating": 5,
"reviewText": "REVIEW FOR SECOND TITLE",
"_id": "58dd8ef46d4aaa16e4545c76",
"createdOn": "2017-03-30T23:04:20.609Z"
}
],
"favouredBy": [
"all"
]
}
]
}
]
my controller for router router.get('/books/:bookid', ctrlbooks.booksReadOne); is
module.exports.booksReadOne = function(req, res) {
console.log('Finding book details', req.params);
if (req.params && req.params.bookid) {
Bok
.findById(req.params.bookid)
.exec(function(err, book) {
if (!book) {
sendJSONresponse(res, 404, {
"message": "bookid not found"
});
return;
} else if (err) {
console.log(err);
sendJSONresponse(res, 404, err);
return;
}
console.log(book);
sendJSONresponse(res, 200, book);
});
} else {
console.log('No bookid specified');
sendJSONresponse(res, 404, {
"message": "No bookid in request"
});
}
};
and this is what I tried to return only the title with a given title id provided but this doesn't work when I tested it using postman
module.exports.tilesReadOne = function(req, res) {
console.log('Finding book details', req.params);
if (req.params && req.params.titleid) {
Bok
.findById(req.params.titleid)
.exec(function(err, book) {
if (!book) {
sendJSONresponse(res, 404, {
"message": "title not found"
});
return;
} else if (err) {
console.log(err);
sendJSONresponse(res, 404, err);
return;
}
console.log(book);
sendJSONresponse(res, 200, book);
});
} else {
console.log('No title specified');
sendJSONresponse(res, 404, {
"message": "No title in request"
});
}
};