![](/img/trans.png)
[英]Operation `users.insertOne()` buffering timed out after 10000ms in, Node.js, MongooseError
[英]mongooseError: Operation `users.insertOne()` buffering timed out after 10000 ms
當我在此代碼中運行 mongoose 時,在我看來好像它沒有及時連接到我的數據庫。 它是本地 mongodb 數據庫而不是圖集。
錯誤:mongooseError:操作users.insertOne()
緩沖在 10000 毫秒后超時
當我沒有注釋掉插入操作時發生,並且在它連接到數據庫一段時間后我將登錄到我的控制台。 當它沒有被注釋掉時,我沒有得到“貓鼬已連接”,而只是上述錯誤。
//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)
當我注釋掉插入新用戶時需要一段時間,但最終它會連接到 bh_db。 有誰知道發生了什么以及解決方案是什么?
// 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.