简体   繁体   中英

mongooseError: Operation `users.insertOne()` buffering timed out after 10000 ms

When i run mongoose in this code it seems to me as if it doesnt connect to my database in time. It is a local mongodb database and not atlas.

The error: mongooseError: Operation users.insertOne() buffering timed out after 10000 ms

occurs when i do not comment out the insert operation, and i will get logged in my console after a while that it has connected to the database. When it has not been commented out, i do not get the "mongoose has been connected", but just the aforementioned error.

//script.js

const mongoose = require('mongoose')
const User = require("./User")

mongoose.connect("mongodb://localhost/bh_db", 
()=>{
    console.log("mongoose has been connected")
}, e => console.error(e))

const user = new User({name:"Kyle", age: 26})
user.save().then( () =>console.log("User Saved"))
//User.js

const mongoose = require('mongoose')

const userSchema = new mongoose.Schema({
    name: String,
    age: Number
})

module.exports = mongoose.model("User", userSchema)

When i comment out the inserting new user it takes a while, but eventually it will connect to bh_db. Does anyone know what is going on and what the solution is?

// Connect to the MongoDB cluster
    try{
        mongoose.connect(
            "mongodb://0.0.0.0:27017/bh_db",
            { useNewUrlParser: true, useUnifiedTopology: true },
            () => console.log("Mongoose is connected"),
        );
    } catch (e) {
        console.log("could not connect");
    }
    const dbConnection = mongoose.connection;
    dbConnection.on("error", (err) => console.log(`Connection error ${err}`));
    dbConnection.once("open", () => console.log("Connected to DB!"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM