[英]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.当我在此代码中运行 mongoose 时,在我看来好像它没有及时连接到我的数据库。 It is a local mongodb database and not atlas.它是本地 mongodb 数据库而不是图集。
The error: mongooseError: Operation users.insertOne()
buffering timed out after 10000 ms错误:mongooseError:操作users.insertOne()
缓冲在 10000 毫秒后超时
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.当我注释掉插入新用户时需要一段时间,但最终它会连接到 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!"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.