我正在学习 nodejs 和 mongodb 与 getting MEAN application
我的系统规范是
ubuntu 16.04(64 位)
Node v 6.1.0
npm v 3.8.6
"mongodb": "^2.1.18",
" Mongoose ": "^4.4.16",
到目前为止,我已经创建了模式并使用 db.locations.save()
通过终端在 mongodb 集合中添加了一个条目,并使用 获取了生成的文档 _id db.locations.find()
.
终端中的 db.locations.find().pretty() 返回低于输出
{
"_id" : ObjectId("57428745e89f5c55e1d057dc"),
// and many other path
}
现在当我在浏览器中打开 /api/locations/57428745e89f5c55e1d057dc 时,它既不给出结果也不结束请求。永久显示加载.. 在使用 postman 进行测试时。
即使尝试使用错误的 Id 也比返回正确的错误
{"message":"Cast to ObjectId failed for value \"57428745e89f5c55e1d057dca\" at path \"_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);
};
最佳答案
我遇到了类似的问题,所以我更新了我的模型文件。例如
const workoutsSchema = new Schema({
_id: mongoose.ObjectId,
email_add: String,
workout_date: String,
workout_duration: String
});
在 Controller 中:-
router.route("/workouts/:id").get(function(req, res) {
console.log("Fetch Workouts of="+req.params.id);
var id = new mongoose.Types.ObjectId(req.params.id);
Workouts.findById(id, function(err, workout) {
if (err)
res.send(err)
res.json(workout);
});
});
关于node.js - mongoose 不使用 findById 返回输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383917/