【发布时间】:2016-09-19 21:59:45
【问题描述】:
我正在和getting MEAN application一起学习nodejs和mongodb
我的系统规格是
ubuntu 16.04(64 位)
节点 v 6.1.0
npm v 3.8.6
"mongodb": "^2.1.18",
"猫鼬": "^4.4.16",
到目前为止,我已经创建了架构并通过终端使用db.locations.save() 在 mongodb 集合中添加了一个条目,并使用db.locations.find() 获取生成的文档 _id。
终端中的 db.locations.find().pretty() 在输出下方返回
{
"_id" : ObjectId("57428745e89f5c55e1d057dc"),
// and many other path
}
现在当我在浏览器中打开 /api/locations/57428745e89f5c55e1d057dc 时,它既没有给出结果也没有结束请求。使用邮递员进行测试时,永久显示正在加载。
即使尝试使用错误的 ID 也不会返回正确的错误
{"message":"转换为 ObjectId 的值失败 \"57428745e89f5c55e1d057dca\" 在路径 \"_id\"","name":"CastError","kind":"ObjectId","value":"57428745e89f5c55e1d057dca","path":"_id"}
并具有正确的 id
console.log(mongoose.Types.ObjectId.isValid(req.params.locationid)); // return true
试过了
findOne({"_id": mongoose.Types.ObjectId(req.params.locationid) })
但没有结果
可能是什么问题? mongoose 中是否有任何错误或缺少任何内容。以下是我的基本文件以及所需代码
loc8r/app.js
require('./app_api/models/db');
var routesApi = require('./app_api/routes/index');
app.use('/api', routesApi);
loc8r/app_api/models/db.js
var mongoose = require( 'mongoose' );
var mongoURI = 'mongodb://localhost/loc8r';
var mongoDB = mongoose.createConnection(mongoURI);
mongoDB.on('connected', function (){
console.log('mongoose connected to ' + mongoURI);
});
require('./locations');
loc8r/app_api/models/locations.js
var mongoose = require( 'mongoose' );
var openingTimeSchema = new mongoose.Schema({
days: {type: String, required: true},
opening: String,
closing: String,
closed: {type: Boolean, required: true}
});
var reviewSchema = new mongoose.Schema({
author: String,
rating: {type: Number, required: true, min: 0, max: 5},
reviewText: String,
createdOn: {type: Date, "default": Date.now}
});
var locationSchema = new mongoose.Schema({
name: {type: String, required: true},
address: String,
rating: {type: Number, "default": 0, min: 0, max: 5},
facilities: [String],
coords: {type: [Number], index: '2dspehere'},
openingTime: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('Location', locationSchema);
loc8r/app_api/router/index.js
var express = require('express');
var router = express.Router();
var ctrlLocations = require('../controllers/locations');
/* Locations pages */
router.get('/locations/:locationid', ctrlLocations.locationsReadOne);
loc8r/app_api/controllers/locations.js
var mongoose = require('mongoose');
var Loc = mongoose.model('Location');
module.exports.locationsReadOne = function (req, res) {
if(req.params && req.params.locationid) {
Loc
.findById(req.params.locationid)
.exec(function(err, location) {
if(err) {
sendJsonResponse(res, 404, err);
return;
}
if (!location) {
sendJsonResponse(res, 404, {"message": "locationid not found"});
return;
}
sendJsonResponse(res, 200, location);
});
} else {
sendJsonResponse(res, 200, {"message": "no location id in request"});
}
}
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
【问题讨论】:
-
对不起,但这并不能解决我的问题。已经搜索过 google 和 mongoose github 问题。
-
你试过其他答案了吗?
-
是的。我创建了新文档并尝试过。没有帮助
-
你确定
locationsReadOne已经被联系到了吗?您是否在应用中使用了某种可能导致请求挂起的中间件?