我正在阅读一本书,介绍如何使用MEAN堆栈开发Web应用程序。当我去localhost:3000 / api / locations /:locationid / reviews /:reviewid我得到“reviewid not found”无论我输入相应的reviewid还是我输入一个随机数,我都会将评论记录到控制台并且它是null。 localhost:3000 / api / locations /:locationid有效,我不知道发生了什么。任何帮助深表感谢。
所以这是我的文档在MongoDB中的样子:
{
"_id" : ObjectId("5a9025348de0d78d8792e105"),
"name" : "Hardee's",
"address" : "200 W Main St, Pilot Mountain, NC 27041",
"rating" : 3,
"facilities" : [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords" : [
-80.469262,
36.385647
],
"openingTimes" : [
{
"days" : "Monday - Saturday",
"opening" : "5:00am",
"closing" : "11:00pm",
"closed" : false
},
{
"days" : "Sunday",
"opening" : "6:00am",
"closing" : "11:00pm",
"closed" : false
}
],
"reviews" : [
{
"author" : "Bigpoppa Eizenga",
"id" : ObjectId("5a902dfd2bb702dcaf1b85e0"),
"rating" : 5,
"timestamp" : ISODate("2018-01-23T05:00:00Z"),
"reviewText" : "Used drive-thru. Service was friendly an d fast, even with 4 special order items. I would definitely return when in that area!"
}
]
}
以下是Node.js显示的内容,无论:reviewid我输入:
> [email protected] start C:\Users\Galen\Desktop\MEAN Stack\loc8r
> node ./bin/www
Mongoose connected to mongodb://localhost/Loc8r
Getting single review
{ reviews:
[ { rating: 5,
createdOn: 2018-02-28T10:56:58.203Z,
author: 'Bigpoppa Eizenga',
id: 5a902dfd2bb702dcaf1b85e0,
timestamp: 2018-01-23T05:00:00.000Z,
reviewText: 'Used drive-thru. Service was friendly and fast, even with 4 special order items. I would definitely return when in that area!' } ],
_id: 5a9025348de0d78d8792e105,
name: 'Hardee\'s' }
null
这是我的控制器,reviews.js:
var mongoose = require('mongoose');
var Loc = mongoose.model('Location');
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
module.exports.reviewsCreate = function (req, res) {
sendJsonResponse (res, 200, {"status" : "success"});
};
module.exports.reviewsReadOne = function(req, res) {
console.log("Getting single review");
if (req.params && req.params.locationid && req.params.reviewid) {
Loc
.findById(req.params.locationid)
.select('name reviews')
.exec(
function(err, location) {
console.log(location);
var response, review;
if (!location) {
sendJsonResponse(res, 404, {
"message": "locationid not found"
});
return;
} else if (err) {
sendJsonResponse(res, 400, err);
return;
}
if (location.reviews && location.reviews.length > 0) {
review = location.reviews.id(req.params.reviewid);
if (!review) {
console.log(review);
sendJsonResponse(res, 404, {
"message": "reviewid not found"
});
} else {
response = {
location: {
name: location.name,
id: req.params.locationid
},
review: review
};
sendJsonResponse(res, 200, response);
}
} else {
sendJsonResponse(res, 404, {
"message": "No reviews found"
});
}
}
);
} else {
sendJsonResponse(res, 404, {
"message": "Not found, locationid and reviewid are both required"
});
}
};
module.exports.reviewsUpdateOne = function (req, res) {
sendJsonResponse (res, 200, {"status" : "success"});
};
module.exports.reviewsDeleteOne = function (req, res) {
sendJsonResponse (res, 200, {"status" : "success"});
};
这是我的路线,index.js:
var express = require('express');
var router = express.Router();
var ctrlLocations = require('../controllers/locations');
var ctrlReviews = require('../controllers/reviews');
//locations
router.get('/locations', ctrlLocations.locationsListByDistance);
router.post('/locations', ctrlLocations.locationsCreate);
router.get('/locations/:locationid', ctrlLocations.locationsReadOne);
router.put('/locations/:locationid', ctrlLocations.locationsUpdateOne);
router.delete('/locations/:locationid', ctrlLocations.locationsDeleteOne);
//reviews
router.post('/locations/:locationid/reviews', ctrlReviews.reviewsCreate);
router.get('/locations/:locationid/reviews/:reviewid', ctrlReviews.reviewsReadOne);
router.put('/locations/:locationid/reviews/:reviewid', ctrlReviews.reviewsUpdateOne);
router.delete('/locations/:locationid/reviews/:reviewid', ctrlReviews.reviewsDeleteOne);
module.exports = router;
我认为这是所有相关信息。我感谢任何帮助。
location.reviews
是一个嵌套数组,所以它不会得到Mongoose的虚拟化id
getter。与任何其他JS数组一样处理它。更改:
review = location.reviews.id(req.params.reviewid);
至:
review = location.reviews.find(review => review.toJSON().id.toString() === req.params.reviewid)
更新:工作代码。