初次搭建NodeJS+MongoDB环境遇MongooseError超时问题求助
解决MongooseError: Operation details.insertOne() buffering timed out after 10000ms错误
错误信息
const err = new MongooseError(message); ^ MongooseError: Operation details.insertOne() buffering timed out after 10000ms
问题代码
app.js
const { request, response } = require('express') const express = require('express'); const hbs = require('hbs'); const app = express(); const mongoose = require("mongoose"); const routes = require('./routes/main'); const Detail = require('./models/Detail'); // /static/css/style.css app.use('/static', express.static("public")) // app.use(express.static("public")) app.use('', routes) // Template Engine HBS app.set('view engine', 'hbs') // this is the path where our all HTML files are available app.set('views', 'views') hbs.registerPartials('views/partials') // MogoDB Connections mongoose.set("strictQuery", false); mongoose.connect("mongodb://localhost/nodejslearning", () => { console.log("database connected") Detail.create( { brandName:"Learn NodeJS", brandIconUrl:"/", links:[ { label:"Home", url:"/", }, { label:"Services", url:"/", }, ] } ) }) app.get('/', (request, response) => { response.send("Wow This is the data from our server") }) app.listen(process.env.PORT | 1111, () => { console.log('our website server is running now') })
Detail.js
const mongoose = require('mongoose'); const Detail = mongoose.Schema({ brandName:String, brandIconUrl:String, navbarLinks:[ { label:String, url:String, } ] }) module.exports = mongoose.model("detail", Detail)
排查与解决步骤
1. 数据库连接回调不保证连接就绪
mongoose.connect的回调仅表示连接操作已发起,不代表数据库连接完全建立。直接在回调内执行Detail.create,可能此时连接还未就绪,Mongoose会将操作放入缓冲队列,最终触发超时。
修复:用Promise链式调用确保连接成功后再执行数据操作:
mongoose.set("strictQuery", false); mongoose.connect("mongodb://localhost/nodejslearning") .then(() => { console.log("database connected"); return Detail.create({ brandName:"Learn NodeJS", brandIconUrl:"/", navbarLinks:[ // 字段名需与Schema匹配 { label:"Home", url:"/", }, { label:"Services", url:"/", }, ] }); }) .then(() => console.log("Detail数据插入成功")) .catch(err => console.error("数据库操作失败:", err));
2. Schema字段与插入数据不匹配
Detail.js的Schema定义的字段是navbarLinks,但app.js插入的数据用的是links,字段名不匹配会导致插入失败,进而触发超时。
修复:将app.js中的links改为navbarLinks,与Schema保持一致。
3. 修复端口逻辑错误
原代码中process.env.PORT | 1111使用了位运算符|,会导致环境变量不存在时出现非预期数值,改为逻辑或||:
app.listen(process.env.PORT || 1111, () => { console.log('our website server is running now') })
修复后的完整app.js
const { request, response } = require('express') const express = require('express'); const hbs = require('hbs'); const app = express(); const mongoose = require("mongoose"); const routes = require('./routes/main'); const Detail = require('./models/Detail'); app.use('/static', express.static("public")) app.use('', routes) // Template Engine HBS app.set('view engine', 'hbs') app.set('views', 'views') hbs.registerPartials('views/partials') // MongoDB Connections mongoose.set("strictQuery", false); mongoose.connect("mongodb://localhost/nodejslearning") .then(() => { console.log("database connected"); return Detail.create({ brandName:"Learn NodeJS", brandIconUrl:"/", navbarLinks:[ { label:"Home", url:"/", }, { label:"Services", url:"/", }, ] }); }) .then(() => console.log("初始Detail数据插入成功")) .catch(err => console.error("数据库操作错误:", err)); app.get('/', (request, response) => { response.send("Wow This is the data from our server") }) app.listen(process.env.PORT || 1111, () => { console.log('our website server is running now') })
内容的提问来源于stack exchange,提问作者Tech World
相关产品推荐
相关产品推荐

