[英]"message": "errorMongooseError: Operation `userinfos.insertOne()` buffering timed out after 10000ms"
I'm currently creating a new API with MongoDB and Express, and I'm currently having this issue "message":我目前正在使用 MongoDB 和 Express 创建一个新的 API,我目前遇到这个问题“消息”:
"errorMongooseError: Operation
userinfos.insertOne()
buffering timed out after 10000ms"“errorMongooseError:操作
userinfos.insertOne()
缓冲在 10000 毫秒后超时”
his is the way that I setup my API calls:他是我设置 API 调用的方式:
const express=require('express');
const router=express.Router();
const Userinfo=require("../Models/Userinfo")
router.get('/',(req,res)=>{
res.send("we are Post")
});
router.get('/Specic', async(req,res)=>{
try{
const data=await Userinfo.find();
console.log(data);
res.json(data)
}
catch(err)
{
res.json({message:err})
}
})
router.post("/",(req,res)=>{
const test= new Userinfo({
"Fname":req.body.Fname,
"Lname":req.body.Lname
});
console.log(test);
test.save().then(data=>{
res.json(data);
}).catch((err)=>{res.json({message:"error"+err})})
})
module.exports=router;
MoDel Defining like this Userinfo.js MoDel 像这样定义Userinfo.js
const mongoose=require('mongoose');
/*const PoistSchema=mongoose.Schema({
Fname:String,
Lname:String,
DOB:Date.now()
});*/
const PostSchema=mongoose.Schema({
Fname:{
type:String,
require:true
},
Lname:{
type:String,
require:true
},
DOB:{
type:String,
default:Date.now
},
});
module.exports=mongoose.model("Userinfo",PostSchema)
App.js应用程序.js
const express=require('express');
const app=express();
const mongoose=require('mongoose');
require("dotenv/config");
const bodyParser=require("body-parser")
///Middlewares
//app.use(auth);
app.use(bodyParser.json());
const postroutes=require("./Routes/Posts");
app.use("/post",postroutes)
app.get('/',(req,res)=>{
res.send("we are om")
})
app.get('/posts',(req,res)=>{
res.send("we are Post")
})
try {
mongoose.connect(process.env.DBConnection,{useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex:true},()=>{
console.log("Sucess");
},(error)=>{
console.log(error);
});
console.log("ConnectedZZ")
} catch (error) {
console.log(error);
}
app.listen(3000);
Is there any suggestion to inset and get data.While getting data not getting any error.是否有任何插入和获取数据的建议。获取数据时没有任何错误。
There can be several reasons for this issue occurred, but let's start with:发生此问题可能有多种原因,但让我们从以下开始:
As We can read in docs正如我们可以在文档中看到的那样
Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.
Mongoose 让您可以立即开始使用您的模型,而无需等待 mongoose 与 MongoDB 建立连接。 That's because mongoose buffers model function calls internally.
这是因为 mongoose 缓冲 model function 内部调用。 This buffering is convenient, but also a common source of confusion.
这种缓冲很方便,但也是一个常见的混淆来源。 Mongoose will not throw any errors by default if you use a model without connecting.
如果您在没有连接的情况下使用 model,ZCCADCDEDB567ABAE643E15DCF0974E503Z 默认不会抛出任何错误。
So it looks like Your model
is being called before the connection is established.所以看起来你的
model
在建立连接之前被调用了。 To handle this error, you should use.catch() or try/catch with async/await .That should solve your problem.要处理此错误,您应该使用 .catch() 或 try/catch 和async/await 。这应该可以解决您的问题。
Here You have example code snippet:在这里,您有示例代码段:
(async () => {
try {
await mongoose.connect(process.env.DBConnection)
console.log('MongoDB connected!!');
} catch (err) {
console.log('Failed to connect to MongoDB', err);
}
})()
you can confirm if the DB is correct before calling the DB calls.您可以在调用 DB 调用之前确认 DB 是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.