我的JSON文件如下,我的路由器 router.get( '/书/:BOOKID',ctrlbooks.booksReadOne); 返回以下JSON这是预期,但如果我想带指定id的发言权只返回标题为这种API router.get(“/书/:BOOKID /标题/:titleid”,ctrlbooks.titlesReadOne) ; 它不会工作我错过了什么?通过ID
[
{
"_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"
]
}
]
}
]
我控制器,路由器 router.get( '/书/:BOOKID',ctrlbooks.booksReadOne); 是
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"
});
}
};
,这就是我试图用提供的给定标题ID只返回标题,但是当我使用邮递员
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"
});
}
};
的可能的复制[只检索MongoDB中集合的对象阵列中的查询元件](http://stackoverflow.com/questions/3985214/retrieve-onl Y型的,查询元素功能于一个对象阵列功能于MongoDB中收集) – Veeram