我正在尝试使用_id
来查询我的MongoDB中的单个文档。我正在使用Mongoose(版本:4.11.1)。当我尝试运行查询时,我通过_id
作为路径localhost:3000/poi/one/595ef9c8c4891179f8b4bbfb
上的URL参数。Express.js&Mongoose查询问题
当我按下回车键后,浏览器将继续加载并永不完成。我能够在控制台中看到“获取一个POI”。但从来没有看到别的。
我正在使用mLab来托管我的数据库。里面只有一个文件。
这里是我的查询代码:
let express = require('express');
let router = express.Router();
let {Poi} = require('../models/db-model');
router.get('/one/:id', (req, res, next) => {
console.log('getting one POI');
Poi.findOne({_id: req.params.id}).exec(function (err, poi) {
if (err) {
res.send('error occurred')
}
else {
console.log(poi);
res.send(poi)
}
});
});
我的数据库模型:
const mongoose = require('mongoose');
// point of interest schema
let poiSchema = mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
trim: true,
},
url: {
type: String,
required: true
},
address: {
type: String,
required: true
},
cost: {
type: Number,
required: false
}
});
// instantiate models
let Poi = mongoose.model('Poi', poiSchema);
// export models
module.exports = {Poi};
我一直在坚持日现在几个小时都有问题,可能只是不知道问题是什么。
编辑:
猫鼬配置:
const mongoose = require('mongoose');
const config = require('../app-config.json');
mongoose.Promise = global.Promise;
// connect to mLab Collection
mongoose.connect(config.dbUri);
module.exports = {mongoose};
打开猫鼬调试,看看查询是否真正发出,以及实际发送给它的值。在服务器启动之前使用'mongoose.set('debug',true)'和mongoose.connect()'在同一个地方。关于这个问题,你是否最近可能因为弃用警告消息而改变了你的'mongoose.connect()'?因为很多人都在错误地改变这种改变,其结果是,由于未解决的回调正在等待,所以没有发出请求。 –