我正在与获取MEAN应用程序一起学习nodejs和mongodb
我的系统规格是
ubuntu 16.04(64位)
节点v 6.1.0
npm v 3.8.6
“ mongodb”:“ ^ 2.1.18”,
“ mongoose”:“ ^ 4.4.16”,
到目前为止,我已经创建的模式,并通过终端使用MongoDB中添加集合中的条目db.locations.save()
,并得到生成的文档_id用db.locations.find()
。
终端中的db.locations.find()。pretty()返回以下输出
{
"_id" : ObjectId("57428745e89f5c55e1d057dc"),
// and many other path
}
现在,当我在浏览器中打开/ api / locations / 57428745e89f5c55e1d057dc时,它既没有给出结果,也没有结束请求。与邮递员一起测试时,永久显示加载中。
甚至尝试使用错误的ID也会返回正确的错误
{“ message”:“在路径\” _ id \“”,“ name”:“ CastError”,“ kind”:“ ObjectId”,“ value”:“ 57428745e89f5c55e1d057dca”的值\“ 57428745e89f5c55e1d057dca \”的对ObjectId的转换失败, “ path”:“ _ id”}
并使用正确的ID
console.log(mongoose.Types.ObjectId.isValid(req.params.locationid)); // return true
甚至尝试过
findOne({"_id": mongoose.Types.ObjectId(req.params.locationid) })
但是没有结果
可能是什么问题?猫鼬中是否有任何bug或缺少任何内容?以下是我的基本文件以及所需的代码
require('./app_api/models/db');
var routesApi = require('./app_api/routes/index');
app.use('/api', routesApi);
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');
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);
var express = require('express');
var router = express.Router();
var ctrlLocations = require('../controllers/locations');
/* Locations pages */
router.get('/locations/:locationid', ctrlLocations.locationsReadOne);
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);
};
从猫鼬文档中获取解决方案
重要的!如果您使用mongoose.createConnection()打开了单独的连接,但是尝试通过mongoose.model('ModelName')访问模型,则该模型将无法正常工作,因为它没有连接到活动的数据库连接。在这种情况下,通过创建的连接访问模型:
所以改变我建立连接的方式
//var mongoDB = mongoose.createConnection(mongoURI);
mongoose.connect(mongoURI);
还需要注释使用mongoDB变量的所有其他函数/表达式
替代方法:(如果您不想在db.js中更改mongoDB)
在controllers / locations.js中
var Loc = mongoose.model('Location');
用下面的线改变
var conn = mongoose.createConnection('mongodb://localhost/loc8r');
var Loc = mongoose.model('Location');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句