Node.js+MongoDB开发:Postman请求无响应API失效问题
Node.js + Postman CRUD请求无响应问题
我是Node.js和Postman的新手,尝试运行CRUD操作时遇到问题:Postman一直显示“发送请求”,无法获取响应。数据库已成功连接,但API似乎无法正常工作。根路径的app.get接口可正常响应,不过通过app.use挂载的/location路由接口无响应。
Postman请求端点:GET http://localhost:7400/location
主文件代码
const express=require('express'); const bodyParser=require('body-parser'); const mongoose=require('mongoose'); const locRoute=require("./routes/location"); mongoose.set('strictQuery', true) const app=express(); const PORT=7400; const MONGO_URI="mongodb://localhost:27017/myrough"; mongoose.connect(MONGO_URI,()=>{ console.log('connected to db'); },e=>console.log("Error",e)); app.use(bodyParser.json()); app.use('/location', locRoute); app.get('/',(req,res)=>res.send('working')) app.listen(PORT,()=>{ console.log(`Server is running on port ${PORT}`); })
Location路由代码
const express=require('express'); const locationController=require('../Controllers/location') const routes=express.Router(); console.log('menu') routes.get('/',locationController.getLocations); module.exports=routes;
Location控制器代码
const locations=require('../Models/location'); exports.getLocations=(req,res)=>{ locations.find().then(result=>{ res.status(200).json({ message:"locations successfully fetched from DB", data:result }) }).catch(error=>{ res.status(500).json({ message:"locations not found", error:error }) }); }
Location模型代码
const mongoose=require('mongoose'); const locationSchema= mongoose.Schema({ name:{ type:String, required:true }, city_id:{ type:String, required:true }, location_id:{ type:String, required:true }, country_name:{ type:String, required:true } }); module.exports=mongoose.model("Locations",locationSchema,"locations");
MongoDB数据
{ "_id": "1", "name": "ShalimarBhagh, Delhi", "city_id": "1", "location_id": "1", "country_name": "India" }, { "_id": "2", "name": "Janpat, Delhi", "city_id": "1", "location_id": "2", "country_name": "India" }
问题排查与解决方法
1. 修正MongoDB _id类型不匹配问题
你的MongoDB数据中_id是字符串类型,但Mongoose默认将_id视为ObjectId类型。执行find()时,Mongoose会尝试将字符串_id转换为ObjectId,导致查询卡住无响应。
解决:在Schema中显式指定_id为String类型:
const locationSchema= mongoose.Schema({ _id: { type: String, required: true }, // 新增该行 name:{ type:String, required:true }, city_id:{ type:String, required:true }, location_id:{ type:String, required:true }, country_name:{ type:String, required:true } });
2. 完善数据库连接逻辑
当前代码在数据库未完全连接时就启动了服务器,可能导致请求时数据库连接未就绪。改用Promise链式调用,确保连接成功后再启动服务:
mongoose.set('strictQuery', true) const app=express(); const PORT=7400; const MONGO_URI="mongodb://localhost:27017/myrough"; mongoose.connect(MONGO_URI) .then(() => { console.log('connected to db'); app.listen(PORT,()=>{ console.log(`Server is running on port ${PORT}`); }); }) .catch(e => console.log("DB Connection Error",e));
3. 添加日志排查请求流程
在控制器中添加日志,确认请求是否进入查询逻辑、是否返回结果:
exports.getLocations=(req,res)=>{ console.log("收到/location GET请求"); locations.find().then(result=>{ console.log("查询结果:", result); res.status(200).json({ message:"locations successfully fetched from DB", data:result }) }).catch(error=>{ console.log("查询错误:", error.message); res.status(500).json({ message:"locations not found", error:error.message }) }); }
4. 验证Postman请求配置
确保Postman发送的是GET请求,地址为http://localhost:7400/location,未添加多余的Headers或参数导致请求异常。
内容的提问来源于stack exchange,提问作者5B8-Sujith
相关产品推荐
相关产品推荐

