Search code examples
node.jsmongodbmongoosenode-modulesmongoose-web-server

MongooseError: Operation `details.insertOne()` buffering timed out after 10000ms


I am facing a issue during learning the first time setup with nodejs and mongoDB.

onst err = new MongooseError(message); ^

MongooseError: Operation details.insertOne() buffering timed out after 10000ms

this is my app.js file data with that i am getting this error.

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')
})

and here is my Detail.js - file code

const mongoose = require('mongoose');
const Detail = mongoose.Schema({
    brandName:String,
    brandIconUrl:String,
    navbarLinks:[
        {
            label:String,
            url:String,
        }
    ]
})

module.exports = mongoose.model("detail", Detail)


Solution

  • It timed out because the connection was not established. Set host and port, and don't use localhost:

    mongoose.connect("mongodb://127.0.0.1:27017/nodejslearning", () => { /*...*/}