我的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