【问题标题】:"message": "errorMongooseError: Operation `userinfos.insertOne()` buffering timed out after 10000ms"“消息”:“errorMongooseError:操作 `userinfos.insertOne()` 缓冲在 10000 毫秒后超时”
【发布时间】:2022-01-26 14:02:13
【问题描述】:

我目前正在使用 MongoDB 和 Express 创建一个新的 API,我目前遇到了这个问题 “消息”:

"errorMongooseError: 操作userinfos.insertOne() 缓冲定时 10000 毫秒后输出”

这是我设置 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;

像这样定义模型 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

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);

是否有任何建议插入和获取数据。获取数据时没有任何错误。

【问题讨论】:

  • 您能介绍一下您的 package.json 吗?也许链接到回购? ;-)

标签: javascript node.js mongodb express mongoose


【解决方案1】:

您可以在调用 DB 调用之前确认 DB 是否正确。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

出现此问题可能有多种原因,但让我们从以下开始:

正如我们在docs中看到的那样

Mongoose 让您无需等待即可立即开始使用模型 用于 mongoose 建立与 MongoDB 的连接。 那是因为猫鼬在内部缓冲了模型函数调用。这 缓冲很方便,但也是常见的混淆来源。 如果你使用模型,Mongoose 默认不会抛出任何错误 无需连接。

所以看起来你的model连接建立之前被调用了。要处理这个错误,你应该使用 .catch() 或 try/catch 和async/await。那应该解决你的问题。


这里有示例代码 sn-p:

(async () => {
  try {
    await mongoose.connect(process.env.DBConnection)
    console.log('MongoDB connected!!');
  } catch (err) {
    console.log('Failed to connect to MongoDB', err);
  }
})()

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2021-04-23
    • 2021-06-29
    • 2021-11-15
    • 2021-12-08
    • 2021-04-01
    • 2021-06-30
    • 2021-04-01
    • 2021-04-12
    相关资源
    最近更新 更多